Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用“缩放大小”区域,从顶部和底部向下一个区域点添加线_R_Ggplot2 - Fatal编程技术网

R 使用“缩放大小”区域,从顶部和底部向下一个区域点添加线

R 使用“缩放大小”区域,从顶部和底部向下一个区域点添加线,r,ggplot2,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"

这个标题毫无意义,但让我解释一下。我的目标是从大的“保持”圆的顶部(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) 
#示例数据帧

df这里有一个尝试,它使用由
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'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()
#示例数据帧

df我不这么认为,至少没有内置的东西。我能想到的唯一方法是部分构建绘图以强制所有刻度进行训练,然后分离训练过的刻度信息以确定每个圆的半径(以适当的单位),然后使用它来计算圆的顶部和底部的位置,并使用该选项指定线段的端点。一点也不琐碎。