r - 使用 ggplot2 调整图表条高度

标签 r ggplot2

<分区>

df2 <- iris[c(5,1)]
df3 <- aggregate(df2$Sepal.Length, list(df2$Species), mean)
names(df3) <- c("x","y")

ggplot(df3, aes(x,y)) +
  geom_bar(aes(fill=x),stat="identity") +
  theme(axis.ticks=element_blank(), axis.text.x=element_blank())

enter image description here

我已经成功地从此图中删除了轴刻度线和标签。我试图摆脱酒吧下方的空白灰色空间。零应该是图表的下限。我一直在寻找一种调整功能来向下拉条或切断底部灰色部分,但没有成功。希望 ggplots 不会与下面的额外空间硬连线。

最佳答案

将以下元素添加到您的代码中(受 this Q & A 启发):

theme_classic()
scale_x_discrete(expand=c(0,0))
scale_y_continuous(expand=c(0,0))

除了 theme_classic(),您还可以使用 theme_bw(),它会在绘图中添加水平线和垂直线。

您的代码应该如下所示:

ggplot(df3, aes(x,y)) +
  geom_bar(aes(fill=x),stat="identity") +
  scale_x_discrete(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  theme_classic() +
  theme(axis.ticks=element_blank(), axis.text.x=element_blank())

这给出:

enter image description here

关于r - 使用 ggplot2 调整图表条高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119897/

相关文章:

r - 矩阵中两个字符串的最大 nchar

r - 为什么这个 ggplot 上的颜色是错误的?

r - 使用 ggplot2 在 R 中绘制等倾线

r - 在时间戳的一列上使用 difftime 计算时间的运行差异

r - 如何检测R中特定范围内的峰值

R Shiny 加速数据加载

r - 增加 ggplot 标题中的下划线大小

r - ggmap, ggimage... - 在 R 中保存和加载 map

r - Dotchart(在 base R 中)或 ggplot2 中具有分组数据的等价物

r - 如何使用响应式(Reactive)对象创建函数?