Use scale_size_area to add the rows above and below to the next point in the region.

The title is not practical, but let me explain. My goal is to make the line from the top of the big circle “Hold” (at 12:00) to the top of the circle “Back” (at 12:00), etc. Down. Then also a line from the bottom of the big circle (6:00) to the bottom of the next big circle, etc. Is it possible to add such lines without manually placing segments? Thank.

# 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

+4
source share
1 answer

, , ggplot, , ggplot. ggplot - . , . , , "" "". , ggplot, grid; , .

, ( ) , .

ggplot - expand = c(0,0) , , .

# 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 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

+2
source

Source: https://habr.com/ru/post/1585011/


All Articles