如何调整列和文本的大小,就像R中列的顶部一样?

brccelvz  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(135)

我有住房的数据框架,但不得不做另一个数据框架,需要总结住房类型取决于房子的面积,其中有2种类型的房屋在3个不同的地区。

df <- na.omit(n[, c("matssvaedi", "teg_eign_s_i")])
counts <- df %>%
  group_by(matssvaedi, teg_eign_s_i)%>%
  summarise(count = n())
df1 <- ggplot(counts, aes(x = matssvaedi, y = count))
df1 + geom_bar(aes(fill = teg_eign_s_i), stat = "identity", position = 'dodge') + geom_text(aes(label = count), color="black",vjust = 0.01, size = 3) + scale_fill_brewer(palette = "Paired") + theme_classic()

pkmbmrz7

pkmbmrz71#

假设您的问题是“如何将文本放置在相应的条形图的顶部?"在这种情况下,您可以向geom_text()添加一个position参数,方法与为geom_bar()指定位置的方法相同,例如,请参阅下面的小reprex:

counts <- mtcars |> 
  group_by(cyl, vs) |> 
  summarise(count = n())
df1 <- ggplot(counts, aes(x = factor(cyl), y = factor(count)))
df1 + geom_bar(aes(fill = vs), stat = "identity", position = 'dodge') + 
  geom_text(aes(label = count, group = vs,), 
            position = position_dodge(width = .9),
            color="black",vjust = 0.01, size = 3)

您可以在geom_text()中使用vjust参数,根据需要将文本稍微上移或下移。

相关问题