改变ggplot2中一个数据集的颜色或厚度

时间:2015-03-16 23:53:31

标签: r colors ggplot2

我正在策划我数据集的效果。

我在我的数据集中添加了一个额外的列(BP),并且能够按船阶段对图表进行着色。

      BoatPhs      fit       se      lower     upper        BP
1         Before 3.685875 0.3287521 3.038621 4.333130     Before
2   After0-20NTA 3.317189 0.6254079 2.085872 4.548506  After0-20
3   After0-20TAA 5.579384 0.5696270 4.457890 6.700878  After0-20
4   After0-20TAP 3.932360 0.4304098 3.084960 4.779760  After0-20
5  After20-40NTA 4.522714 0.7771793 2.992586 6.052842 After20-40
6  After20-40TAA 4.505207 0.5500699 3.422217 5.588196 After20-40
7  After20-40TAP 3.602183 0.3880538 2.838174 4.366192 After20-40
8    ApproachNTA 4.039599 0.5688482 2.919638 5.159560   Approach
9    ApproachTAA 4.421112 0.5176408 3.401969 5.440255   Approach
10   ApproachTAP 4.497809 0.3978328 3.714547 5.281071   Approach
# Speed plot
Spdplot <- ggplot(y, aes(x=BoatPhs, y=fit, colour=BP, ymax=max(fit)*1.05)) + geom_point(position = position_dodge(width = 0.5, height = 0), size = 2) + geom_errorbar(aes(ymin=fit-se, ymax=fit+se), position = position_dodge(width = 0.5, height = 0), width = 0.5) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+

#run Spdplot
Spdplot

#sets x values in results order + relables x + y axes + changes colours
Spdplot + scale_x_discrete(limits=c("Before","ApproachNTA","ApproachTAA","ApproachTAP","After0-20NTA","After0-20TAA","After0-20TAP", "After20-40NTA","After20-40TAA","After20-40TAP")) + 
  xlab("Boat Phase") + ylab("Log of Group Travel Speed") + 
  annotate("text", x=6, y=6.2, label="***") +
  annotate("text", x=4, y=4.95, label="*") + 
  theme(axis.title.x = element_text(vjust=-0.5), axis.title.y = element_text(vjust= 1), axis.text.x = element_text(size=12), axis.text.y = element_text(size=12)) +
  theme(legend.title = element_blank()) + 
  scale_colour_manual(values=c("#999999","#666666","#CCCCCC","#000000"), 
                      breaks=c("Before", "Approach", "After0-20", "After20-40"),
                      labels=c("Before", "Approach", "After0-20", "After20-40"))

我想做的是能够使之前阶段的点和误差条更粗,更大胆。有谁知道如何改变这一点的细节?还是多个不同点?我知道如何为多个小组改变它们,但我只想提出一点比其他小组更突出。

干杯

1 个答案:

答案 0 :(得分:0)

如果您首先向y添加另一个变量并仅生成前阶段T,则可以有条件地加粗,加粗并为其着色。以下代码仅显示颜色。

y$bold <- c(T, rep(F, 9))

以下是仅使用红色的情节:

Spdplot <- ggplot(y, aes(x=BoatPhs, y=fit, colour=BP, ymax=max(fit)*1.05)) +
  geom_point(position = position_dodge(width = 0.5, height = 0), size = 2) +
  geom_errorbar(aes(ymin=fit-se, ymax=fit+se, color = ifelse(y$bold == T, "green", "black")), position = position_dodge(width = 0.5, height = 0),
                width = 0.5) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

enter image description here