【问题标题】:Group Barplot failure组条形图失败
【发布时间】:2018-12-14 18:18:06
【问题描述】:

我正在分析来自 YouGov 调查的 R 中的数据,该调查查看变量,然后将它们与受访者所在的美国各州进行比较。

例如

                repimmigration
states           Much.less Somewhat.less      Same Somewhat.More Much.More
  Alabama        12.500000     10.000000 25.000000     22.500000 30.000000
  Alaska         25.000000     25.000000  8.333333     16.666667 25.000000
  Arisona        12.820513     17.094017 11.965812     17.094017 41.025641
  Arkansas       12.000000      6.000000 18.000000     22.000000 42.000000
  California     21.985816     10.638298 21.276596     16.548463 29.550827
  Colorado       20.588235     20.588235 17.647059     14.705882 26.470588
  Connecticut    14.285714     23.809524 16.666667     21.428571 23.809524

然后我添加了 rowSums 以使 5 'repimmigration' 变成 3 ' Easier, Same, Harder'

                   Less      Same     More
Alabama        22.50000 25.000000 52.50000
Alaska         50.00000  8.333333 41.66667
Arisona        29.91453 11.965812 58.11966
Arkansas       18.00000 18.000000 64.00000
California     32.62411 21.276596 46.09929
Colorado       41.17647 17.647059 41.17647
Connecticut    38.09524 16.666667 45.23810
Delaware       36.36364 36.363636 27.27273

我正在尝试挑选 6 个特定的州“爱荷华州、俄亥俄州、宾夕法尼亚州、威斯康星州、密歇根州、佛罗里达州”并将它们放入一个组条形图中。 但是,每当我尝试它根本不组合在一起时,我知道我的过程有一个错误,我就是不知道在哪里。问题似乎源于添加的“rowSums”函数。

这是我的脚本:

Rep.immig.states=prop.table(table(states,repimmigration),1)*100
rep.im.sum = data.frame(Less=rowSums(Rep.immig.states[,1:2]), Same=Rep.immig.states[,3], More=rowSums(Rep.immig.states[,4:5]))

statesrepim = data.frame(Iowa=rep.im.sum['Iowa',1:3], Florida=rep.im.sum['Florida',1:3], Michigan=rep.im.sum['Michigan',1:3], Ohio=rep.im.sum['Ohio',1:3], Pennsylvania=rep.im.sum['Pennsylvania',1:3], Wisconsin=rep.im.sum['Wisconsin',1:3])

barplot(as.matrix(statesrepim),beside=T)

【问题讨论】:

  • 您能否使您的示例可重现,即从其 url 获取数据集的 post 代码?或者在数据集上发布dput() 的输出,对于某些州?
  • 当你说 “挑选 6 个特定状态并将它们放入一个组条形图中” 时,这听起来不像 ggplot 意义上的“分组”。只需选择/过滤数据框中的行。

标签: r bar-chart


【解决方案1】:

是否必须使用基础图?以下使用ggplot2

# Load your toy data
df <- read.table(text = "Less      Same     More
Alabama        22.50000 25.000000 52.50000
Alaska         50.00000  8.333333 41.66667
Arisona        29.91453 11.965812 58.11966
Arkansas       18.00000 18.000000 64.00000
California     32.62411 21.276596 46.09929
Colorado       41.17647 17.647059 41.17647
Connecticut    38.09524 16.666667 45.23810")

# Load packages
library("tidyverse")
library("ggplot2")

tidy_df <- 
  df %>% 
  mutate(state = rownames(df)) %>% 
  gather(key='category', value = "value", -state)

# Plot your data group bar plots
ggplot(tidy_df, aes(category, value)) +   
  geom_bar(aes(fill = state),
          position = "dodge", stat="identity")

如果您只想绘制某些状态,您只需在绘制之前对数据进行子集化,如下所示:

tidy_df <- 
  df %>% 
  mutate(state = rownames(df)) %>% 
  gather(key='category', value = "value", -state) %>%
  filter(state %in% c("Alabama", "Connecticut"))

ggplot(tidy_df, aes(category, value)) +   
  geom_bar(aes(fill = state),
           position = "dodge", stat="identity")

【讨论】:

    【解决方案2】:

    考虑继续使用 base R 的barplot

    数据 (下图采用这种结构)

    txt <- '                   Less      Same     More
    Alabama        22.50000 25.000000 52.50000
    Alaska         50.00000  8.333333 41.66667
    Arizona        29.91453 11.965812 58.11966
    Arkansas       18.00000 18.000000 64.00000
    California     32.62411 21.276596 46.09929
    Colorado       41.17647 17.647059 41.17647
    Connecticut    38.09524 16.666667 45.23810
    Delaware       36.36364 36.363636 27.27273'
    
    df <- read.table(text=txt, header = TRUE)
    

    图表

    # OPEN TO FILE FOR WRITING
    png("/path/to/my/graph.png", width = 800, height = 350)
    
      # INITALIZE CANVAS
      layout(c(1,2), heights=c(7,1))
    
      # BAR PLOT
      par(mar=c(4, 4, 4, 4))
    
      barplot(as.matrix(df), col=rainbow(nrow(df)), main="State Value Bar Graph",
              beside=TRUE, cex.axis=0.8, ylim=c(0,80), ylab="Value")
    
      # LEGEND
      par(mar=c(0, 0, 0, 0))
      plot.new()
      legend("top", legend=row.names(df), fill=rainbow(nrow(df)), ncol=nrow(df))
    
    dev.off()
    


    对于特定状态,只需索引row.names。请参阅 states 向量的使用,相应地调整调色板(rainbow):

      # INITALIZE CANVAS
      layout(c(1,2), heights=c(7,1))
    
      # BAR PLOT
      par(mar=c(4, 4, 4, 4))
      states <- c("Arizona", "California", "Delaware")
    
      barplot(as.matrix(df[states,]), col=rainbow(length(states)), main="State Value Bar Graph",
              beside=TRUE, cex.axis=0.8, ylim=c(0,80), ylab="Value")
    
      # LEGEND
      par(mar=c(0, 0, 0, 0))
      plot.new()
      legend("top", legend=row.names(df[states,]), fill=rainbow(length(states)),
             ncol=nrow(df[states,]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2011-03-01
      • 2016-04-08
      相关资源
      最近更新 更多