在ggplot中着色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ggplot中着色相关的知识,希望对你有一定的参考价值。
这是数据集:
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轴之间。请注意,由于其他颜色的堆叠,有两种不同的蓝色调。我也不想摆脱透明度,因为我似乎无法让它看起来很好。
此图包含两种不同的蓝色调:
使用建议的修补程序绘制代码,不会生成图例。
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"))
答案
简短的回答是,根据您的数据,没有简单的方法可以满足您的要求(请参阅@camille的评论)。但是,也许我们可以修复“让它看起来很好”的部分。我认为“with alpha”和“without 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"))
这给了我们:
请记住,我是colourblind,所以颜色选择可能会有显着改善 - 但你得到漂移。你也可以玩线条大小,或者这只是你的开发维度的一个问题。
以上是关于在ggplot中着色的主要内容,如果未能解决你的问题,请参考以下文章
R语言ggplot2可视化:使用R原生plot函数为指定曲线下面的区域着色ggplot2可视化在曲线的特定下方添加分割线ggplot2为指定曲线下面的区域着色