首页 > 解决方案 > 使用 R 绘制图形

问题描述

我正在尝试使用 R 在以下数据表的形状和完整平均值之间绘制条形图现在我的问题是如何绘制条形图。我试过了,但它不起作用

    (shp <- tibble(Type   = c("1", "2", "3", "4"),  shapes    = c("square", "rectangle", "rectangle", "square"),  in_num = c(500, 800, 900, 1200),  complete = c(4, 9, 3, 8)
))
    shp %>% group_by(shapes) %>% summarise(Avg = mean(complete))
    ggplot(data = shp, mapping = aes(x = shapes, y = Avg)) +geom_bar()

标签: r

解决方案


添加stat = "identity"到您的geom_bar(). summarise并在命令后加上右括​​号。

 shp %>% 
  group_by(shapes) %>% 
  summarise(Avg = mean(complete)) %>%
  ggplot(aes(x = shapes, y = Avg, fill = shapes)) +
  geom_bar(stat = "identity", show.legend = F) +
  theme_bw()

推荐阅读