首页 > 解决方案 > 从 ggplot geom_crossbar 中删除等高线

问题描述

下面是一个数据集示例和带有 geom_crossbar 的绘图代码,它让我接近我正在寻找的东西,除了我找不到删除条形周围黑色轮廓线的方法。

data = data.frame(age = c(10,12,14,16,18), 
                  height_min = c(120,140,148,150,150),
                  height_max = c(150,165,172,175,175))
ggplot(data) +
  geom_crossbar(aes(ymin= height_min, ymax=height_max, x=age, y=height_min),
                fill = "gray70", fatten=0) + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) +
  labs(x=expression(paste("Age (years)")), 
       y=expression(paste("Height (cm)"))) +
  theme(plot.margin = unit(c(1,1,1,1), "cm")) +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=30,face="bold"))

如何删除条形周围的黑色轮廓线?

标签: rggplot2geom-bar

解决方案


您可以在 geom_crossbar 中添加“linetype = 0”或“color = NA”。如果您不需要导出绘图,“size=0”也可以工作(否则条形消失)。

以下是可能对其他人有用的代码:

data = data.frame(age = c(10,12,14,16,18), 
                  height_min = c(120,140,148,150,150),
                  height_max = c(150,165,172,175,175))
ggplot(data) +
  geom_crossbar(aes(ymin= height_min, ymax=height_max, x=age, y=height_min),
                fill = "gray70", fatten=0, linetype = 0) + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) +
  labs(x=expression(paste("Age (years)")), 
       y=expression(paste("Height (cm)"))) +
  theme(plot.margin = unit(c(1,1,1,1), "cm")) +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=30,face="bold"))

感谢 dc37 的评论!


推荐阅读