在单个图中绘制40条曲线

时间:2016-01-19 18:24:02

标签: r ggplot2

我不确定这是一个编程问题还是数据可视化问题。如果您认为其他网站更合适,请告诉我们。无论如何,我有以下数据框:

GRID FLOW NITER    tau
   0  100     1 0.6152
   0  100 35001 0.6152
   0  100 69001 0.6152
   1  100     1 0.6105
   1  100 35001 0.6106
   1  100 69001 0.6106
   2  100     1 0.6147
   2  100 35001 0.6147
   2  100 69001 0.6147
   3  100     1 0.6151
   3  100 35001 0.6153
   3  100 69001 0.6153
   4  100     1 0.6105
   4  100 35001 0.6105
   4  100 69001 0.6105
   5  100     1 0.6140
   5  100 35001 0.6142
   5  100 69001 0.6142
   6  100     1 0.6130
   6  100 35001 0.6129
   6  100 69001 0.6129
   7  100     1 0.6152
   7  100 35001 0.6152
   7  100 69001 0.6152
   8  100     1 0.6098
   8  100 35001 0.6097
   8  100 69001 0.6097
   9  100     1 0.6143
   9  100 35001 0.6143
   9  100 69001 0.6143
  10  100     1 0.6123
  10  100 35001 0.6123
  10  100 69001 0.6123
  11  100     1 0.6148
  11  100 35001 0.6150
  11  100 69001 0.6150
  12  100     1 0.6155
  12  100 35001 0.6154
  12  100 69001 0.6154
  13  100     1 0.6152
  13  100 35001 0.6154
  13  100 69001 0.6154
  14  100     1 0.6154
  14  100 35001 0.6154
  14  100 69001 0.6154
  15  100     1 0.6152
  15  100 35001 0.6153
  15  100 69001 0.6153
  16  100     1 0.6162
  16  100 35001 0.6162
  16  100 69001 0.6162
  17  100     1 0.6150
  17  100 35001 0.6152
  17  100 69001 0.6152
  18  100     1 0.6160
  18  100 35001 0.6160
  18  100 69001 0.6160
  19  100     1 0.6150
  19  100 35001 0.6152
  19  100 69001 0.6152
  20  100     1 0.6150
  20  100 35001 0.6149
  20  100 69001 0.6149
  21  100     1 0.6150
  21  100 35001 0.6150
  21  100 69001 0.6150
  22  100     1 0.6150
  22  100 35001 0.6150
  22  100 69001 0.6150
  23  100     1 0.6152
  23  100 35001 0.6153
  23  100 69001 0.6153
  24  100     1 0.6150
  24  100 35001 0.6152
  24  100 69001 0.6152
  25  100     1 0.6156
  25  100 35001 0.6156
  25  100 69001 0.6156
  26  100     1 0.6152
  26  100 35001 0.6154
  26  100 69001 0.6154
  27  100     1 0.6158
  27  100 35001 0.6159
  27  100 69001 0.6159
  28  100     1 0.6151
  28  100 35001 0.6153
  28  100 69001 0.6153
  29  100     1 0.6160
  29  100 35001 0.6159
  29  100 69001 0.6159
  30  100     1 0.6146
  30  100 35001 0.6148
  30  100 69001 0.6147
  31  100     1 0.6143
  31  100 35001 0.6145
  31  100 69001 0.6145
  32  100     1 0.6151
  32  100 35001 0.6153
  32  100 69001 0.6153
  33  100     1 0.6151
  33  100 35001 0.6153
  33  100 69001 0.6153
  34  100     1 0.6164
  34  100 35001 0.6163
  34  100 69001 0.6163
  35  100     1 0.6172
  35  100 35001 0.6172
  35  100 69001 0.6172
  36  100     1 0.6162
  36  100 35001 0.6163
  36  100 69001 0.6163
  37  100     1 0.6164
  37  100 35001 0.6165
  37  100 69001 0.6165
  38  100     1 0.6157
  38  100 35001 0.6157
  38  100 69001 0.6157
  39  100     1 0.6157
  39  100 35001 0.6157
  39  100 69001 0.6157
  40  100     1 0.6197
  40  100 35001 0.6197
  40  100 69001 0.6197

我需要制作一个图表,让读者看到GRID = 0的曲线和所有其他网格之间的差异(或缺少)。目标受众是技术性的(这是一篇科学论文)。我在ggplot中尝试了以下内容:

df$GRID <- as.factor(df$GRID)
p <- ggplot(df, aes(x = NITER, y = tau, linetype = GRID ))
p + geom_line() +
    ggtitle("tau") +
    labs(x="", y="") 

结果并不是那么好:

enter image description here

首先,图例只包含12个符号,而其余28条曲线根本没有绘制。其次,文字非常小。切换到,

p <- ggplot(df, aes(x = NITER, y = tau, color = GRID ))

现在至少R绘制所有曲线:

enter image description here

但是,区分不同的曲线非常困难。更糟糕的是,在很容易发现GRID = 0曲线(它是唯一的连续线)之前,现在很难找到它。我能做什么?另一种解决方案可能是使用相同的颜色和相同的线型绘制所有曲线,但是在每条曲线的顶部添加曲线名称(0,1,2,... 40),而不是使用图例。我不知道该怎么做:另外,我需要一些方法来覆盖共享相同端点的两条曲线的名称。

2 个答案:

答案 0 :(得分:4)

鉴于您的数据,您需要考虑不同的美学(轴)和几何(点)是否更合适。

一种方法是在x轴上使用GRID,在y轴上使用tau,并绘制NITER个彩色点,同时减少数据点与相同值的重叠tau NITERggplot(df,aes(x = GRID, y = tau, colour = factor(NITER))) + geom_point(position = position_dodge(w = 0.3)) 稍微改变位置:

private View getSubCategoryListItemView(final SubCategoryItem si, double dwPercentage, final int cat_id) {
    LayoutInflater li = LayoutInflater.from(this);
    View vv = li.inflate(R.layout.sub_cat_list_item, llCatListHolder, false);
    ImageView ivIcon = (ImageView) vv.findViewById(R.id.iv_sub_cat_icon);
    final TextView tvName = (TextView) vv.findViewById(R.id.tv_sub_cat_name);
    ivIcon.setImageResource(AppConstants.ALL_CAT_MARKER_ICONS[cat_id-1]);
    ViewGroup.LayoutParams lpIv = ivIcon.getLayoutParams();
    lpIv.width = (int) (primaryIconWidth * dwPercentage);
    ivIcon.setLayoutParams(lpIv);
    tvName.setText(si.getSubcatHeader());
    tvName.setTextSize((float) (VIEW_WIDTH * .10 * dwPercentage));

    /**
     *
     *This OnClickListener will be called for clicking subcategory items from the top list
     */
    //  tvName.setTextColor(Color.WHITE);
    vv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ArrayList<SubCategoryItem> subCategoryItems = getSubCategoryList(cat_id);
            for(SubCategoryItem si : subCategoryItems) {
                //    tvName.setTextColor(Color.WHITE);
            }
            tvName.setTextColor(Color.RED);
            /*code for category*/
            /*following code will be different for each category*/
            /*category id 1 means education.
            * category id 2 means health
            * category id 3 means entertainment
            * category id 4 means government
            * category id 5 means legal
            * category id 6 means financial
            * category id 7 means job*/
            // tvName.setTextColor(Color.WHITE);
            switch (currentCategoryID) {
                case AppConstants.EDUCATION:
                    ArrayList<EducationServiceProviderItem> eduItem = constructEducationListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithEducationInfo(si.getSubcatHeader(), cat_id, eduItem);
                    break;
                case AppConstants.HEALTH:
                    //TODO write necessary codes for health
                    ArrayList<HealthServiceProviderItem> healthItem = constructHealthListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithHealthInfo(si.getSubcatHeader(), cat_id, healthItem);
                    break;
                case AppConstants.ENTERTAINMENT:
                    tvName.setTextColor(Color.GREEN);
                    ArrayList<EntertainmentServiceProviderItem> entItem = constructEntertainmentListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithEntertainmentInfo(si.getSubcatHeader(), cat_id, entItem);
                    break;
                //TODO write necessary codes for entertainment
                case AppConstants.GOVERNMENT:
                    //TODO write necessary codes for government
                    break;
                case AppConstants.LEGAL:
                    ArrayList<LegalAidServiceProviderItem>legalItem = constructlegalaidListItemForHeader(cat_id,si.getSubcatHeader());
                    callMapFragmentWithLegalAidInfo(si.getSubcatHeader(),cat_id,legalItem);
                    break;
                case AppConstants.FINANCIAL:
                    ArrayList<FinancialServiceProviderItem> financialItem = constructfinancialListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithFinancialInfo(si.getSubcatHeader(), cat_id, financialItem);
                    break;
                case AppConstants.JOB:
                    ArrayList<JobServiceProviderItem> jobItem = constructjobListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithJobInfo(si.getSubcatHeader(), cat_id, jobItem);
                    break;
                default:
                    break;
            }
            /*code for all*/
            showSubCatListItem.setEnabled(true);
            subCatItemListHeader.setText(si.getSubcatHeader());
            constructSubCategoryItemList(cat_id, si.getSubcatHeader());
        }
    });
    return vv;
}

enter image description here

答案 1 :(得分:3)

作为替代方案,如果你只是试图将网格0与其他所有网格进行比较,你可以使用这样的变体。我不确定这看起来很棒,但玩各种美学可能会改善它。

## Make a variable: one vs the rest
dat$grp <- factor(1L + (dat$GRID == 0))

library(ggplot2)
library(ggthemes)  # theme_tufte
lbls <- c('0', 'Other')
ggplot(dat) +
    geom_line(aes(NITER, tau, group=GRID, color=grp, alpha=grp, size=grp)) +
    theme_tufte() +
    scale_size_manual('Grid', breaks=2:1, values=c(.9, 1.1), labels=lbls) +
    scale_color_manual('Grid', breaks=2:1, values=c('grey0', 'red'), labels=lbls) +
    scale_alpha_manual('Grid', breaks=2:1, values=c(0.1, 1), labels=lbls)

enter image description here