【发布时间】:2017-09-25 20:47:39
【问题描述】:
我需要创建一个类似于此的气泡图:
我使用ggplot2 使用来自this post 的代码创建了一个单边气泡图。
这在右侧创建了 y 轴和 x 轴,但我需要在两侧都有 x 轴。有什么建议吗?
这是我的代码:
grid <- read.csv("data.csv", sep=",")
grid$Variability <- as.character(grid$Variability)
grid$Variability <- factor(grid$Variability, levels=unique(grid$Variability))
grid$Research <- as.character(grid$Research)
grid$Research <- factor(grid$Research, levels=unique(grid$Research))
grid$Contribution <- as.character(grid$Contribution)
grid$Contribution <- factor(grid$Contribution, levels=unique(grid$Contribution))
library(ggplot2)
ggplot(grid, aes(Research, Variability))+
geom_point(aes(size=sqrt(grid$CountResearch*2 /pi)*7.5), shape=21, fill="white")+
geom_text(aes(label=CountResearch),size=4,hjust=0.5,vjust=0.5)+
scale_size_identity()+
theme(panel.grid.major=element_line(linetype=2, color="black"),
axis.title.x=element_text(vjust=-0.35,hjust=1),
axis.title.y=element_text(vjust=0.35),
axis.text.x=element_text(angle=0,hjust=0.5,vjust=0.5) )
数据样本:
structure(list(Variability = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("C",
"R", "D", "A"), class = "factor"),
Research = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Op",
"Maint", "Evol", "Re", ""), class = "factor"),
CountResearch = c(5L, 21L, 12L, 3L, NA, 1L, 1L, 6L, NA, NA,
NA, 16L, 27L, 30L, NA, 22L, 4L, 18L, 4L, NA), Contribution = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L), .Label = c("Struct", "Log", "Func",
"Synt", "Behav"), class = "factor"), CountContribution = c(12L,
27L, 5L, 25L, 13L, 0L, 8L, 1L, 1L, 3L, 59L, 37L, 8L, 71L,
2L, 22L, 5L, 0L, 23L, 22L)), .Names = c("Level", "Research",
"CountResearch", "Contribution", "CountContribution"), row.names = c(NA,
-20L
), class = "data.frame")
【问题讨论】:
-
请提供一个可重现的例子,例如你的数据。例如,您可以粘贴 dput(data) 的输出,以及您创建的 ggplot2 代码。stackoverflow.com/questions/5963269/…
-
按要求更新了正文。
-
@user8042455 不,你没有。请使用
dput提供您的数据样本。 -
对不起,我不明白这个 dput.. 我是新来使用 R 代码和 ggplot2
-
粘贴
dput(grid)的输出,其中grid 是您在ggplot2 中绘制的长格式数据框。如果网格很大,则只提供一个子集,例如dput(gridsub)