在dotplot上绘制圆圈以标记ggplot2中的最佳区域

时间:2013-10-10 10:53:20

标签: r ggplot2 scatter-plot

我有一个具有x和y属性的发电厂的点图。最好的发电厂是那些x和y都很高的发电厂。我现在想要想象一下我的地块的哪些区域是合乎需要的并且不太适合发电厂

我需要制作的是这样的: enter image description here

有什么想法吗?我尝试使用geom_point绘制巨大的点,但是根据我的绘图的导出分辨率改变大小我也尝试绘制像here这样的圆圈但是如果它的部分落在实际绘图之外,ggplot不能正确绘制圆圈

1 个答案:

答案 0 :(得分:2)

使用@joran的圆圈功能here时,这似乎有效:

# prepare data for circles
green <- circleFun(center = c(5, 5), diameter = 1.5, npoints = 100)
yellow <- circleFun(center = c(5, 5), diameter = 3, npoints = 100)
orange <- circleFun(center = c(5, 5), diameter = 4.5, npoints = 100)
red <- circleFun(center = c(5, 5), diameter = 6, npoints = 100)

dat <- rbind(green, yellow, orange, red)

# specify levels to get the order of colours right,
# and set the plotting order from large (red) to small (green) circle
dat$fill <- factor(rep(c("green", "yellow", "orange", "red"), each = 100),
                   levels = rev(c("green", "yellow", "orange", "red")))

# dummy data for points
dat2 <- data.frame(x = rnorm(100, mean = 3), y = rnorm(100, mean = 3))

ggplot(data = dat, aes(x = x, y = y)) +
  geom_polygon(aes(fill = fill)) +
  geom_point(data = dat2, aes(x = x, y = y)) +
  coord_cartesian(xlim = c(0, 5), ylim = c(0, 5)) + 
  scale_fill_manual(values = rev(c("green", "yellow", "orange", "red")))

enter image description here 效果不佳的事情:
由于某些原因,当(1)使用theme(legend.position = "none")关闭图例时,以及(2)使用scale_fill_identity()而不是scale_fill_manual()从'填充'中选择颜色时,圆圈会失真'dat'中的变量。我不知道为什么。