如何在ggplot2中的数据点上方和下方绘制列

时间:2017-10-16 04:52:31

标签: r plot ggplot2 line point

使用ggplot2,我绘制了三个地点的15种物种的百分比值(每个物种都出现在每个地点)。与站点“C”相关的数据点是我的参考点。

现在,我不想将网站“A”和“B”绘制为点,而是希望使用垂直线或列状结构来显示它们。因此,这些数据点应该作为垂直线延伸到站点“C”点(绿色)的顶部或底部,即,在顶部,其中值大于参考值,而底部是较小的值。

具体来说,我希望从红点到绿点的红线和从蓝点到绿点的蓝线。理想情况下,红线的宽度应与红点的宽度相同(蓝色相同)。该线也应该像红色和蓝色点(相对于绿点)一样被偏移,因此线不重叠。最后,线不应该到达中心而应该到达点的边缘。

为此,我有'A'和'B'的偏移点,并且还将它们的大小减小到参考点大小的一半。

library(ggplot2) 
MyData$species <- as.character(MyData$species)
MyData$species <- factor(MyData$species, levels=unique(MyData$species))

pos <- position_dodge(width=0.21)
cols <- c("C" = "darkgreen", "B" = "blue", "A" = "red")

tiff(file = "MyData.tiff", height=10, width=10, units="in", res=300, compression="lzw")
ggplot(data = MyData, aes(x=species, y=value, group=site, colour=site)) +
  geom_point(data=subset(MyData, site=="C"), size = 4, shape=15, alpha=1, position=pos) + 

  geom_line(data=subset(MyData, site=="C"), size = 2, lwd=2, alpha=0.4, show_guide=FALSE) +

  geom_point(data=subset(MyData, site!="C"), size = 1.8, shape=15, alpha=1, position = pos) + 
  scale_colour_manual(values = cols) +
  xlab("Species") +
  ylab("Value (%)") + 
  scale_y_continuous(expand=c(0.01,0.01),
                     limits=c(0.0,100),   
                     breaks=c(0,20,40,60,80,100),
                     labels=c("0","20","40","60","80","100")) + 
  theme_bw() +
  theme(legend.position="none") +
  theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16),
        axis.text.x = element_text(vjust=0.4, size=14, angle=90, hjust=1.0)) +
  theme(axis.title.y = element_text(vjust=0.1,face="bold", size=16),
        axis.text.y = element_text(face="bold", size=14, angle=0)) +
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +
  theme(panel.border = element_rect(size=1, color = "black")) +
  theme(plot.margin = unit(c(0.3,0.4,0.3,0.3),"lines"))
dev.off()

这是我目前的情节。所以基本上,我想用延伸到绿点的线代替红色和蓝色点(不重叠它们)。

enter image description here

非常感谢您提供有关优雅解决方案的任何建议。

这是我的数据集的dput()。

structure(list(site = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B", "C"), class = "factor"), 
    species = structure(c(13L, 11L, 2L, 14L, 1L, 9L, 12L, 10L, 
    6L, 8L, 15L, 7L, 3L, 4L, 5L, 13L, 11L, 2L, 14L, 1L, 9L, 12L, 
    10L, 6L, 8L, 15L, 7L, 3L, 4L, 5L, 13L, 11L, 2L, 14L, 1L, 
    9L, 12L, 10L, 6L, 8L, 15L, 7L, 3L, 4L, 5L), .Label = c("Species 1", 
    "Species 10", "Species 11", "Species 12", "Species 13", "Species 14", 
    "Species 15", "Species 2", "Species 3", "Species 4", "Species 5", 
    "Species 6", "Species 7", "Species 8", "Species 9"), class = "factor"), 
    value = c(2, 3.25, 3.53, 4.31, 4.59, 5.26, 6.02, 6.42, 6.6, 
    7.26, 8.89, 12.45, 35.62, 72.42, 73.55, 1.36, 2.36, 2.17, 
    10.34, 6.84, 1.88, 5.09, 7.35, 3.87, 10.55, 6.6, 14.64, 39.57, 
    88.06, 64.54, 5.03, 12.34, 5.42, 3.63, 5.16, 6.04, 3, 8.94, 
    3.28, 7.64, 6.25, 21.96, 39.35, 78.55, 47.35)), .Names = c("site", 
"species", "value"), class = "data.frame", row.names = c(NA, 
-45L))

2 个答案:

答案 0 :(得分:2)

您可以尝试使用geom_linerange()作为从A / B点到C点的行。

定义每个站点/物种的ymin / ymax值,&amp;重新排序网站,使A / B线下降到C点的每一边:

库(dplyr)

MyData <- MyData %>%
  group_by(species) %>%
  mutate(value.C = value[site == "C"]) %>%
  rowwise() %>%
  mutate(value.min = min(value, value.C),
         value.max = max(value, value.C)) %>%
  ungroup() %>%
  mutate(site = factor(site, levels = c("A", "C", "B")))

简介:

# vary dodge width such that the lines drop to the edge of point C
# for your chosen dimensions (for mine 0.5 was about right)
pos <- position_dodge(width = 0.5) 

ggplot(data = MyData,
       aes(x = species, y = value, 
           ymin = value.min, ymax = value.max, 
           group = site, colour = site, size = site)) +

  geom_linerange(size = 1.8, alpha = 0.4, position = pos) +

  geom_line(data = subset(MyData, site == "C"),
            size = 2, lwd = 2, alpha = 0.4) +
  geom_point(data = subset(MyData, site == "C"),
             size = 4, shape = 15, position = pos) +
  scale_color_manual(values = cols) +
  theme_classic() +
  theme(legend.position = "none")
  # + other theme-related settings...

plot

答案 1 :(得分:1)

您可以添加geom_line来绘制垂直线

library(ggplot2) 
MyData$species <- as.character(MyData$species)
MyData$species <- factor(MyData$species, levels=unique(MyData$species))

pos <- position_dodge(width=0.21)
cols <- c("C" = "darkgreen", "B" = "blue", "A" = "red")

windows()
ggplot(data = MyData, aes(x=species, y=value, group=site, colour=site)) +
  geom_point(data=subset(MyData, site=="C"), size = 4, shape=15, alpha=1, position=pos) + 
  geom_line(data=subset(MyData, site=="C"), size = 2, lwd=2, alpha=0.4, show_guide=FALSE) +
  geom_point(data=subset(MyData, site!="C"), size = 1.8, shape=15, alpha=1, position = pos) + 
  geom_line(aes(group = species)) + #New code Added
  scale_colour_manual(values = cols) +
  xlab("Species") +
  ylab("Value (%)") + 
  scale_y_continuous(expand=c(0.01,0.01),
                     limits=c(0.0,100),   
                     breaks=c(0,20,40,60,80,100),
                     labels=c("0","20","40","60","80","100")) + 
  theme_bw() +
  theme(legend.position="none") +
  theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16),
        axis.text.x = element_text(vjust=0.4, size=14, angle=90, hjust=1.0)) +
  theme(axis.title.y = element_text(vjust=0.1,face="bold", size=16),
        axis.text.y = element_text(face="bold", size=14, angle=0)) +
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +
  theme(panel.border = element_rect(size=1, color = "black")) +
  theme(plot.margin = unit(c(0.3,0.4,0.3,0.3),"lines"))