【问题标题】:I am not able to print the bar graph in R using ggplot2我无法使用 ggplot2 在 R 中打印条形图
【发布时间】:2021-11-05 05:07:41
【问题描述】:
library(ggplot2)
library(dslabs)
library(dplyr)
data("movielens")

movielens %>% top_n(10) %>% ggplot(aes(title, rating)) +
  geom_point()

我希望这段代码能给出一个条形图,就像电影标题和相应评级之间的“movielens”数据框中的 10 部电影的图表,而 这段代码给了我这样的错误:

Error: stat_count() can only have an x or y aesthetic

【问题讨论】:

    标签: r dataframe data-science data-visualization


    【解决方案1】:

    跟进@Basti 的回答。您也可以使用geom_col(),它的作用与geom_bar(stat = "identity") 相同,而无需明确说明。

    参考:https://ggplot2.tidyverse.org/reference/geom_bar.html

    【讨论】:

      【解决方案2】:

      如果您打算为每个title 生成带有ratings 的条形图,则需要提供geom_bar(stat="identity"),因为默认情况下stat="count"

      movielens %>% top_n(10) %>% ggplot(aes(title, rating)) +geom_bar(stat="identity")
      

      【讨论】:

        猜你喜欢
        • 2021-03-14
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多