我有以下数据框:
dput(df)
structure(list(convSlope = c(-0.00103951046958614, -0.000107780395196407,
1.32997761896617e-05, 2.1532762564503e-05, 6.08499370031235e-05,
6.23082414012136e-05, 0.00063868810766659, 0.00137104097264597,
0.00138245637682735, 0.00212793023058462, 0, -6.63582853150368e-05,
-2.60306132968389e-05, -1.9000297997136e-05, 1.80689291471755e-05,
2.41022501949015e-05, 7.68020027582176e-05, 0.000159577537629169,
0.000239631350647912, 0.00036196002617637, 0.00210600195850549,
0), convLow = c(-0.00864332669999781, -0.0155723308541684, -0.00638165550725293,
-0.00647730940467806, -0.00794574690736345, -0.00686551134820207,
-0.00727589426769797, -0.00488657681436086, -0.00868933527765364,
-0.00412964481046931, -0.0167115021724442, -0.00123221754733313,
-0.00373838196002056, -0.00562239375188502, -0.00160896039442434,
-0.00237678286364215, -0.0012737092234363, -0.0016402758202216,
-0.000878360581920508, -0.000871311121398701, 0.000976025059018948,
-0.012527643603177), convHigh = c(0.006565044642425, 0.015400484776708,
0.00641124352688413, 0.00651094254026463, 0.00809335314637886,
0.00698331294640221, 0.00856088786112379, 0.00763765126215838,
0.0114540113784824, 0.00838692108878951, 0.0167115021724442,
0.00110057404955679, 0.00368574375564408, 0.00559370035927454,
0.00164832488535601, 0.00242147584626002, 0.00142461464553302,
0.00196038270799053, 0.00136194408638254, 0.00159190939357683,
0.00323693188998354, 0.012527643603177), siteID = c("A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "E", "E", "E", "E",
"E", "E", "E", "E", "E", "E", "E"), ID = c("Zyg", "Cho", "Pap",
"Bom", "Tor", "Pyr", "Las", "Noc", "Gel", "Geo", "all", "Pap",
"Las", "Gel", "Zyg", "Tor", "Bom", "Cho", "Noc", "Pyr", "Geo",
"all")), class = "data.frame", row.names = c("13", "12", "11",
"10", "9", "8", "6", "3", "2", "1", "14", "15", "141", "131",
"91", "81", "71", "51", "41", "31", "17", "16"))
我创建了以下情节:
ggplot(df,aes(x= ID, y=convSlope))+
geom_hline(yintercept=0, linetype="solid", color = "grey")+
geom_point( col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", "#194CB2",
"#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
"#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900",
"#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), size= 5)+
geom_pointrange(col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3",
"#194CB2", "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
"#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900",
"#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), aes(ymin= convLow,
ymax=convHigh))+
geom_hline(yintercept=0.01, linetype="dashed", color = "grey")+
geom_hline(yintercept=-0.01, linetype="dashed", color = "grey")+
facet_grid(. ~ siteID)+
theme_bw() +
xlab("") +
ylab("Frequency per year")+
theme(strip.text.x = element_text(size=12,
face="bold"),
axis.text.x=element_text(size=rel(1.2), angle=45, hjust = 1),
axis.title.x=element_text(size=rel(2)),
axis.title.y=element_text(size=rel(1.75)),
axis.text.y=element_text(size=rel(1.75)),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank())
我希望点颜色从暗逐渐变亮(暗=最左边的点,亮=最右边的点),以使“全部”的点是最暗的点,而“ Zyg”的点是最亮的点。站点A应从紫色逐渐变为浅蓝色。同样,站点B应该从深绿色逐渐淡入浅绿色。我在col
的{{1}}参数中按从暗到亮的顺序列出了这些颜色,但是在图形输出中颜色的排序不同。
我需要一个解决方案来针对每个站点将这些颜色从暗到亮进行排序。
答案 0 :(得分:1)
这是我要实现的目标:
aes(colour=colour)
和scale_colour_identity()
df <- df[order(df$siteID, df$ID), ]
df$colour <- c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", "#194CB2",
"#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
"#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900",
"#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00")
ggplot(df,aes(x= ID, y=convSlope, colour = colour))+
geom_hline(yintercept=0, linetype="solid", color = "grey")+
geom_point(size= 5)+
geom_pointrange(aes(ymin= convLow, ymax=convHigh))+
geom_hline(yintercept=0.01, linetype="dashed", color = "grey")+
geom_hline(yintercept=-0.01, linetype="dashed", color = "grey")+
facet_grid(. ~ siteID)+
scale_colour_identity() +
theme_bw() +
xlab("") +
ylab("Frequency per year")+
theme(strip.text.x = element_text(size=12,
face="bold"),
axis.text.x=element_text(size=rel(1.2), angle=45, hjust = 1),
axis.title.x=element_text(size=rel(2)),
axis.title.y=element_text(size=rel(1.75)),
axis.text.y=element_text(size=rel(1.75)),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank())
结果:
答案 1 :(得分:0)
解决这类问题的常用方法是将定义颜色的列变成一个因素,并根据需要对级别进行排序。在这种情况下,可能只需要对代码进行少量更改的解决方案是可能的:将siteID排列为所需顺序,然后将其转变为排列顺序中的一个因数。
df %>%
arrange(siteID, ID) %>%
mutate(ID = fct_inorder(ID)) %>%
ggplot(aes(x= ID, y=convSlope))+
geom_hline(yintercept=0, linetype="solid", color = "grey")+
geom_point( col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", "#194CB2",
"#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
"#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900",
"#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), size= 5)+
geom_pointrange(col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3",
"#194CB2", "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
"#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900",
"#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), aes(ymin= convLow,
ymax=convHigh))+
geom_hline(yintercept=0.01, linetype="dashed", color = "grey")+
geom_hline(yintercept=-0.01, linetype="dashed", color = "grey")+
facet_grid(. ~ siteID)+
theme_bw() +
xlab("") +
ylab("Frequency per year")+
theme(strip.text.x = element_text(size=12,
face="bold"),
axis.text.x=element_text(size=rel(1.2), angle=45, hjust = 1),
axis.title.x=element_text(size=rel(2)),
axis.title.y=element_text(size=rel(1.75)),
axis.text.y=element_text(size=rel(1.75)),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank())