使用scale_size_area,从顶部和底部添加线到下一个区域点

时间:2015-04-28 02:20:18

标签: r ggplot2

标题不实,但让我解释一下。我的目标是从大的“保持”圆圈(在12:00)的顶部到“后退”圆圈的顶部(在12:00)有一条线,依此类推。然后还有一条从大圆底部(6:00)到下一个最大圆底部的线,依此类推。是否可以在不手动放置线段的情况下添加此类线路?谢谢。

# example data frame
df <- data.frame(Documents = c(1.5e5, .6e5, .3e5, .1e5), stages = c("Hold", "Back", "Trust", "Camp"), x = c(12, 18, 25, 35), y = c(10, 8, 7, 6))

library("ggplot2")
library(ggthemes)
ggplot(df, aes(x = x, y = y, size = Documents)) +
  geom_point(color = "grey30", alpha = 0.3) + theme_tufte() +
  scale_size_area(max_size = 35) + 
  geom_text(aes(label = stages), size = 6, color = "red") + 
  theme(axis.text = element_blank()) +
  labs(x = "", y = "", fill = "Documents") +
  theme(legend.position = "bottom") + 
  xlim(8, 38) + ylim(5, 13) 

enter image description here

1 个答案:

答案 0 :(得分:2)

Here's an attempt that takes the plot generated by ggplot but then draws the lines inside a viewport with the same scales as the ggplot plot panel. ggplot's internal data gives the size of the circles in points - I think. I think it is the radius of the circle. Thus to get the top and bottom of the circle, I needed to add and subtract "native" units to "points" units. I don't know of a way to do that in ggplot proper, but it can be done in grid; hence the viewports.

But you will note that the lines (and points) are not exactly on the circumference of all circles - not sure of the reason.

I made one minor change to your ggplot - expand = c(0,0) on the two axes so that the viewport can be given the same scales as the plot panel.

# example data frame
df <- data.frame(Documents = c(1.5e5, .6e5, .3e5, .1e5), stages = c("Hold", "Back", "Trust", "Camp"), x = c(12, 18, 25, 35), y = c(10, 8, 7, 6))

library("ggplot2")
library(ggthemes)
library(grid)

p = ggplot(df, aes(x = x, y = y, size = Documents)) +
  geom_point(color = "grey30", alpha = 0.3)  + theme_tufte() +
  scale_size_area(max_size = 35) + 
  geom_text(aes(label = stages), size = 6, color = "red") + 
  theme(axis.text = element_blank()) +
  labs(x = "", y = "", fill = "Documents") +
  theme(legend.position = "bottom") + 
  scale_x_continuous(limits = c(8,38), expand = c(0,0)) +
  scale_y_continuous(limits = c(5,13), expand = c(0,0))

p

# Get the size of the dots from ggplot's internal data.
# I think "size" is radius in "pts".
g = ggplot_build(p)
df$size = g$data[[1]]$size

# Set up viewport for the plot panel
current.vpTree() # Find the plot panel
downViewport("panel.6-4-6-4")

# Set up scales in the plot panel - see "limits" in ggplot
pushViewport(dataViewport(yscale = c(5,13), xscale = c(8,38)))

# Draw points and lines 
grid.points(x = unit(df$x, "native"), y = unit(df$y, "native") + unit(df$size, "pt"), pch = 19, gp = gpar(col = "blue", cex = .5), default.units = "native") 
grid.points(x = unit(df$x, "native"), y = unit(df$y, "native") - unit(df$size, "pt"), pch = 19, gp = gpar(col = "blue", cex = .5), default.units = "native") 
grid.lines(x = unit(df$x, "native"), y = unit(df$y, "native") + unit(df$size, "pt"), gp = gpar(col = "blue", lwd = 2), default.units = "native") 
grid.lines(x = unit(df$x, "native"), y = unit(df$y, "native") - unit(df$size, "pt"), gp = gpar(col = "blue", lwd = 2), default.units = "native") 

popViewport()
popViewport()
popViewport()

enter image description here