请记住,我对R非常陌生。我有一个来自民意调查的数据集,并希望通过气泡图来表示答案,尽管这些数据是分类的,而不是数字。
从数据集"Arab4“中,我有一个问题/变量"Q713”,所有的观察值都编码为1、2、3、4或5作为响应选项。我想画出气泡(由“国家”堆叠在另一个上面),气泡的大小对应于答案获得的选票份额的百分比。例如,如果在以色列,49%的受访者对问题"Q“中的选项1投了赞成票,那么气泡大小将代表49%,并且位于以色列类别标签的上方,气泡的颜色对应于响应类型(1、2、3、4或5)。
我有下面的代码,给了我一个空白的图表,我知道最终要使用带有更多规范的"points“命令。我需要的帮助是从我拥有的数据中定义圆的半径。
plot(Arab4$Country, Arab4$Q713, type= "n", xlab = FALSE, ylab=FALSE)
points(Arab4$country, Arab4$q713)下面是数据集中的一些数据
dput(Arab4$q713[1:50])
structure(c(3, 5, 3, 3, 1, 3, 5, 5, 5, 5, 3, 2, 2, 3, 1, 1, 4,
2, 3, 5, 5, 5, 2, 5, 4, 2, 5, 2, 5, 3, 5, 5, 2, 2, 5, 2, 1, 2,
1, 2, 5, 3, 4, 5, 1, 1, 1, 4, 5, 3), labels = structure(c(1,
2, 3, 4, 5, 98, 99), .Names = c("Promoting democracy", "Promoting economic
development",
"Resolving the Arab-Israeli conflict", "Promoting women’s rights",
"The US should not get involved", "Don't know (Do not read)",
"Decline to answer (Do not read)")), class = "labelled")任何想法都会有帮助!谢谢!
发布于 2017-11-28 09:46:20
正如其他人评论的那样,这真的不是一个气泡图,因为你只有2个维度,圆圈的大小并没有增加任何东西(除了视觉上的吸引力)。但有了这份免责声明,这里有一种我认为您正在努力实现的方法。这需要ggplot2和reshape2库。
library(ggplot2)
library(reshape2)
# create simulated data
dat <- data.frame(Egypt=sample(c(1:5), 20), Libya=sample(c(1:5),20))
# tabulate
dat.tab <- apply(dat, 2, table)
dat.long <- melt(dat.tab)
colnames(dat.long) <- c("Response", "Count", "Country")
ggplot(dat.long, aes(x=Country, y=Count, color=Country)) +
geom_point(aes(size=Count))编辑这里是另一种方法,使用dplyr包的数据操作工具来让你一直到比例:
# using dat from above again
dat.long <- melt(dat)
colnames(dat.long) <- c("Country", "Response")
dat.tab <- dat.long %>%
group_by(Country) %>%
count(Response) %>%
mutate(prop = prop.table(n))
ggplot(dat.tab, aes(x=Country, y=prop, color=Country)) +
geom_point(aes(size=prop))如果不需要的值(98,99)确实是不需要的,那么您需要做一些额外的工作来删除它们。
hth。
https://stackoverflow.com/questions/47521744
复制相似问题