自定义R中的条形图[重复]

z4iuyo4d  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(124)
    • 此问题在此处已有答案**:

How to display all x labels in R barplot?(4个答案)
9小时前关门了。
我想在条形图中按组排序绘制测试结果。我想插入所有可能的结果(0到50点),即使没有人达到那个结果。
我得到了这个,但我想要真正长度的空格。例如,在30到41之间。

team=c("m","w","m","w","w","m","m","w","m","w","m","w","m","m","m","m","m","w","w","m")
pts=c(12,27,6,26,29,16,23,30,20,17,41,14,8,9,5,7,28,42,6,27)`
 
df <- data.frame(team,pts)
df

barplot(table(df$team, df$pts), las=1, beside=TRUE,border="white",
axis.lty = 1,  xlim=c(0,50), ylim= c(0,2),axes=FALSE,
col=c("lightblue","pink")
)

现在我想知道,如何自定义x轴,以便显示所有可能的结果?

wydwbb8l

wydwbb8l1#

ggplot解决方案如下所示:

1.加载包,创建数据框
library(tidyverse)  # install.packages("tidyverse") if you don't have tidyverse

df <- data.frame(
  team=c("m","w","m","w","w","m","m","w","m","w","m","w","m","m","m","m","m","w","w","m"),
  pts=c(12,27,6,26,29,16,23,30,20,17,41,14,8,9,5,7,28,42,6,27)
  )
2.用x轴的整个范围(从0到50)绘制条形图
df |> 
  ggplot(aes(x = pts, fill = team), color = "white") +
  geom_bar(position = "dodge") +
  theme_minimal() +
  scale_x_continuous(limits = c(0, 50),
                     expand = c(0, 0)) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.15)))

3.结果

希望这是有帮助的。

相关问题