【问题标题】:How to create bar chart in r, which has one categorical variable on y axis and continuous variable on x axis? [duplicate]如何在 r 中创建条形图,它在 y 轴上有一个分类变量,在 x 轴上有一个连续变量? [复制]
【发布时间】:2021-05-18 21:49:00
【问题描述】:
data <- read.csv("/Users/infinite/Desktop/covid_19_india.csv", header = TRUE)
table(data$State.UnionTerritory)
barplot(table(data$State.UnionTerritory), ylab = 'No. of confirmed cases', main= "Bar plot 
of covid19 cases")

但我想在 x 轴上绘制已确认病例变量,在 y 轴上绘制 State.UnionTerritory。我想使用两个变量来绘制。但是我尝试使用的方法只有一个变量,其频率在 y 轴上。请帮帮我。

注意:这个文件有两个变量 1. State.UnionTerritory, 2. Confirmed

我想使用条形图将它们相互绘制。

【问题讨论】:

    标签: r ggplot2 visualization


    【解决方案1】:

    您将此问题标记为 ggplot2 在这种情况下,基本代码是:

    library("ggplot2")
    ggplot(data=data) +
        geom_col(aes(x=confirmed, y=State.UnionTerritory))
    

    在基础 R 中认为它类似于此

    barplot(height=data$confirmed, names=data$State.UnionTerritory, horiz=T , las=1)
    

    PD:尽量避免使用基础 R 使用的名称(在本例中为“数据”)命名变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多