【问题标题】:bubble plot using ggplot2使用ggplot2的气泡图
【发布时间】: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)

标签: r plot ggplot2


【解决方案1】:

将数据框转换为长格式并将用于 x 轴的所有变量放在同一列中可能更有意义。此后,您可以一次性绘制 ggplot 中的所有内容,使用 facet 区分贡献和研究:

library(dplyr)
grid2 <- rbind(grid %>% 
                 select(Variability, Research, CountResearch) %>%
                 rename(type = Research, count = CountResearch) %>%
                 mutate(facet = "Research") %>%
                 na.omit(),
               grid %>% 
                 select(Variability, Contribution, CountContribution) %>%
                 rename(type = Contribution, count = CountContribution) %>%
                 mutate(facet = "Contribution"))

ggplot(grid2,
       aes(x = type, y = Variability, 
           size = count, label = count)) +
  geom_point(shape = 21, fill = "white") +
  geom_text(size = 3) +
  scale_size(range = c(5, 20), guide = F) +
  facet_grid(~facet, scales = "free_x") +
  theme(panel.grid.major = element_line(linetype = 2, color = "black"))

(注意:我根据我的图像尺寸看起来合理的尺寸调整了尺寸范围。您的分辨率可能会有所不同。)

【讨论】:

  • 谢谢!工作得很好.. 但是,有没有办法让 y 轴位于两个面之间的中间?
  • @user8042455 不是由 ggplot 设计的,但您可以查看解决方法here
  • @user8042455 我之前也遇到过类似的问题,或者您可以将 y 轴标签分别绘制为 geom_label,以及将“贡献”面板和“研究”面板分别绘制为单独的 ggplot 对象,然后使用cowplot 将它们并排排列。这将允许您将 y 轴标签放在中间。见下文。
【解决方案2】:

这是复制上述所需图的一种可能解决方案。分别绘制每个单独的组件,包括 ggplot2 中的 y 标签为 geom_label,然后使用 cowplot 将它们对齐并排列成一个绘图。

我使用了grid2 长格式数据,并根据@Z.Lin 提供的初始代码构建

library(dplyr)
grid2 <- rbind(grid %>% 
             select(Level, Research, CountResearch) %>%
             rename(type = Research, count = CountResearch) %>%
             mutate(facet = "Research") %>%
             na.omit(),
           grid %>% 
             select(Level, Contribution, CountContribution) %>%
             rename(type = Contribution, count = CountContribution) %>%
             mutate(facet = "Contribution"))

# Find min and max count sizes to scale bubbles
Research.max <- max(grid2[grid2$facet == "Research", ]$count, na.rm = T)
Contribution.max <- max(grid2[grid2$facet == "Contribution", ]$count, na.rm = T)
Research.min <- min(grid2[grid2$facet == "Research", ]$count, na.rm = T)
Contribution.min <- min(grid2[grid2$facet == "Contribution", ]$count, na.rm = T)

# Plot each component in ggplot2, set similar parameters and theme components for each plot with adjustments to position
library(ggplot2)

Research <- ggplot(grid2[grid2$facet == "Research", ], 
               aes(x = type, y = Level, size = count, label = count)) +
  xlab("Research") + ggtitle("") +
  geom_point(shape = 21, fill = "white") +
  geom_text(size = 3) +
  scale_radius(range = c(4, (Research.max+Research.min+4)/2), guide = F) +
  theme(panel.grid.major = element_line(linetype = 2, color = "black"),
    axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), hjust = 1),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "white"),
    plot.margin = margin(t=1,r=0.5,b=1,l=0, unit="cm"))

Contribution <- ggplot(grid2[grid2$facet == "Contribution", ], 
                   aes(x = type, y = Level, size = count, label = count)) +
  xlab("Contribution") + ggtitle("") +
  geom_point(shape = 21, fill = "white") +
  geom_text(size = 3) +
  scale_radius(range = c(4, (Contribution.max+Contribution.min+4)/2), guide = F) +
  scale_y_discrete(position = "right") + 
  theme(panel.grid.major = element_line(linetype = 2, color = "black"),
    axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), hjust = 0),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "white"),
    plot.margin = margin(t=1,r=0,b=1,l=0.5, unit="cm"))

Y_axis <- ggplot(grid2[grid2$facet == "Contribution", ], 
             aes(x = "", y = Level, label = Level)) +
  geom_label(size = 10) +  
  xlab("") + ggtitle("Level") +
  theme(panel.grid.major.y = element_line(linetype = 2, color = "black"),
    panel.grid.major.x = element_line(linetype = 1, color = "black"),
    axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0), hjust = 0.5),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "white"),
    plot.margin = margin(t=1,r=0,b=1,l=0, unit="cm"),
    plot.title = element_text(hjust=0.5))

# Arrange ggplot objects side-by-side using cowplot, define relative widths
library(cowplot)
plot_grid(Contribution, Y_axis, Research, align = "v", axis = "tb", nrow = 1, rel_widths = c(5/10, 1/10, 4/10))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多