ggplot中的阴影

时间:2018-12-04 17:36:29

标签: r ggplot2 graphing shading

这是数据集:

structure(list(Date2 = structure(c(17702, 17703, 
17704, 17705, 
17707, 17711, 17715, 17716, 17716, 17716, 17717, 
17719, 17722, 
17728, 17736, 17738, 17738, 17738, 17739, 17741, 
17749, 17756, 
17757, 17758, 17759, 17760, 17760, 17760, 17762, 
17768, 17770, 
17771, 17772, 17773, 17774, 17774, 17774, 17775, 
17776), class = "Date"), 
Type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
1L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 
2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 
3L), .Label = c("K2SO4_17", 
"H2O_17", "Vac_17"), class = "factor", scores = 
structure(c(-36.8015441939299, 
-169.195587838832, -15.9745482460074), .Dim = 3L, 
.Dimnames = list(
c("H2O_17", "K2SO4_17", "Vac_17")))), mean_value 
= c(91.408737890633, 
4.2275822390544, 13.9666861975414, 11.7769850314945, 
9.48516623659864, 
13.1129305059853, 18.1416455043672, 20.4954137544164, 
195.444253367338, 
88.2036381498178, 7.02512304971248, 8.5167688405357, 
12.4581917402187, 
6.95313557438933, 8.90003612124658, 42.8626456690929, 
201.440665555189, 
20.0066370953826, 11.0691308980722, 13.6405283949989, 
9.78187202268978, 
10.349700146351, 9.1947286440288, 10.3610867117625, 
9.85080655497064, 
44.2794448189869, 125.343063057495, 11.1787073324726, 
10.4234407725895, 
9.66316247064878, 28.0099731759905, 13.0834417978341, 
11.4785164687325, 
5.65472508542547, 39.5686725332234, 154.554369375308, 
6.99683113513417, 
6.40467469581727, 3.88640514173422), se = 
c(56.658001167569, 
2.58885483283402, 1.93200787805785, 1.59088650666074, 
0.458746175914399, 
0.688301838822706, 2.50261347192418, 4.9669759128794, 
56.4904545919887, 
34.027450304715, 2.12027850766817, 2.37315110421232, 
5.51988739202576, 
1.92980153604852, 2.57462580378077, 13.5102180510921, 
59.523760377122, 
4.82585739331836, 2.94452462398769, 3.8952629135035, 
2.66696610575249, 
2.3294237593578, 1.80928331150276, 1.37743135944939, 
1.63654997665711, 
13.2984157319028, 32.5785380784761, 1.3900966734445, 
2.03893363992392, 
4.32316890789433, 11.1683121765524, 1.89291882660845, 
1.56533217770458, 
0.747355908089244, 6.47797452115071, 
20.1508954565148, 0.510625489349245, 
0.55146765698527, 1.64453736266876)), .Names = 
c("Date2", 
"Type", "mean_value", "se"), class = c("tbl_df", 
"tbl", "data.frame"
), row.names = c(NA, -39L))

使用此代码,我生成带有阴影的图形:

ggplot(agg_data, aes(x = Date2, y = mean_value, fill = Type)) +
  geom_area(aes(fill=Type),position = 'identity',alpha=0.4) +
  geom_line(aes(color = Type), lwd = 2) +
  geom_point(shape = 21,aes(fill=Type),lwd=3)+
  geom_errorbar(aes(ymin = mean_value - se, ymax = mean_value + se), 
                lwd = 1, width = 1)

这是生成的图。这里的问题是我希望阴影位于线之间,而不是每条线和x轴之间。请注意,由于其他颜色的叠加,有两种不同的蓝色阴影。我也不想摆脱透明性,因为我似乎无法使它看起来很好。

此图包含两种不同的蓝色阴影:

Figure one: contains two different shades of blue

使用建议的修复方法绘制代码,不会生成图例。

    ggplot(agg_data, aes(x = Date2, y = mean_value, fill = Type)) +
  geom_area(aes(fill=Type),position = 'identity') +
  geom_line(aes(color = Type), lwd = 2) +
  geom_errorbar(aes(ymin = mean_value - se, ymax = mean_value + se), lwd = 
  1, width = 1) +
  geom_point(shape = 16,aes(color=Type),lwd=6)+
  guides(fill=guide_legend(title= "Legend"))+
  scale_fill_manual(name="Type",
                     breaks=c("K2SO4_18", "H2O_18", "Vac_18"),
                     labels=c("Adsorbed", "Inaccessible", "Mobile"),values=c("H2O_17"="palevioletred","K2SO4_17"="darkolivegreen3","Vac_17"="deepskyblue"))+
  scale_colour_manual(name="Type",
                     breaks=c("K2SO4_18", "H2O_18", "Vac_18"),
                     labels=c(expression(paste("K"[2],"SO"[4])), 
expression(paste("H"[2],"O")), "Pore water"),values=c("H2O_17"="palevioletred","K2SO4_17"="darkolivegreen3","Vac_17"="deepskyblue"))+
  xlab("")+
  ylab("")+
  guides(color = guide_legend(title= "Matrix" ),
        fill = guide_legend(title= "N Distribution",override.aes = 
  list(color = NA)))+
  coord_cartesian(ylim=c(0,260))+
  theme(legend.direction = 'vertical', 
    legend.key = element_rect(size=2),
    legend.key.size = unit(3, 'lines'),
    panel.grid.major = element_blank(), 
    legend.title=element_text(size=25,colour='black'),
    panel.grid.minor = element_blank(),
    legend.text=element_text(size=20),
    axis.title.y= element_text(size=40, colour='black'),
    axis.text.y = element_text(size=40, colour='black'),
    axis.ticks = element_line(colour = "black", size=2),
    axis.ticks.length = unit(0.3, "cm"),
    axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank(),
    panel.border = element_rect(colour = "black", fill=NA, size=2),
    plot.title = element_text( size=21, face="bold.italic"),
    plot.margin=unit(c(1,1,-0.5,1), "cm"))

1 个答案:

答案 0 :(得分:1)

简短的回答是,对于给定的数据,没有简单的方法可以满足您的要求(请参阅@camille的评论)。但是,也许我们可以修复“使其看起来不错”的部分。 我认为“有alpha”和“无alpha”之间的一个重要区别是填充颜色与线条颜色不同。好吧-我们可以手动更改它!

ggplot(agg_data, aes(x = Date2, y = mean_value, 
                     fill = Type)) +
  geom_area(aes(fill=Type),position = 'identity') +
  geom_line(aes(color = Type), lwd = 2) +
  geom_point(shape = 21,aes(fill=Type),lwd=3)+
  geom_errorbar(aes(ymin = mean_value - se, ymax = 
                      mean_value + se), lwd = 1, width = 1)+
  scale_fill_manual(values=c("H2O_17"="palevioletred","K2SO4_17"="darkolivegreen3","Vac_17"="deepskyblue"))+
    scale_colour_manual(values=c("H2O_17"="palevioletred4","K2SO4_17"="darkolivegreen4","Vac_17"="deepskyblue3"))

哪个给了我们

That looks better already!

请记住,我是色盲的,因此颜色选择可能会得到显着改善-但是您会发现漂移。您还可以调整行大小,或者这仅仅是开发人员尺寸的问题。