我正在从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个“重新移民”设置为3个“更容易,相同,更困难”
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)
答案 0 :(得分: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")
答案 1 :(得分:1)
考虑继续使用基数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,]))