【问题标题】:Change thickness of a marker in ggplot2更改ggplot2中标记的厚度
【发布时间】:2013-07-14 16:50:12
【问题描述】:

我正在使用以下代码制作一个与外部特征(总计)成比例点的地图,但我想更改标记的宽度。

p <- ggplot()
p <- p + geom_polygon( data=all_states, aes(x=LONG*-1, y=LAT, group = ID),colour="black",              fill="white" )
p <- p + geom_point( data=mydata, aes(x=long*-1, y=lat, size = Total),color="mediumblue",     shape=1) +
scale_size(range = c(1,11), name="Sells Volume")+
labs(title="Reglone SL")+
xlab(" ")+
ylab(" ")
p

【问题讨论】:

  • 我同意 agstudy。还请包含更多关于您的意思的描述...您是指geom_point 生成的标记吗?您已经通过变量Total 对它们进行了缩放,所以您希望它们都更大吗?您希望它们的尺寸变化更大吗?

标签: r map ggplot2


【解决方案1】:

您要更改的是空心点边界的厚度吗?可以使用 grid 包中的 grid.edit 来完成。

library(ggplot2)
library(grid)

ggplot(data = data.frame(x = 1:10, y = 1:10), aes(x=x, y=y)) + 
   geom_point(size = 10, shape = 1)

grid.force()  # To make the grobs visible to grid editing tools

grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = seq(1, 5.5, .5)))

EDIT获取图例键以匹配点

library(ggplot2)
library(grid)
library(gtable)

p = ggplot(data = data.frame(x = 1:10, y = 1:10, c = c(rep("a", 5), rep("b", 5))), 
   aes(x=x, y=y, colour = c)) + 
   geom_point(shape = 1, size = 10)

lwd = 8   # Set line width

g = ggplotGrob(p); dev.off()  # Get the plot grob

# Get the indices for the legend: t = top, r = right, ...
indices <- c(subset(g$layout, name == "guide-box", select = t:r))

# Get the row number of the legend in the layout
rn <- which(g$layout$name == "guide-box")

# Extract the legend
legend <- g$grobs[[rn]]

# Get the legend keys
pointGrobs = which(grepl("points", legend$grobs[[1]]$grobs))

# Check them out - no line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])

# Set line width
for (i in pointGrobs) legend$grobs[[1]]$grobs[[i]]$gp$lwd = lwd

# Check them out - line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])

# Put the modified legend back into the plot grob
g = gtable_add_grob(g, legend, t=indices$t, l=indices$l)

# g$grobs[[4]]$children[[2]]$gp$lwd = lwd  # Alternative for setting lwd for points in the plot

grid.newpage()
grid.draw(g)

grid.force()  # To make the grobs visible to grid editing tools

grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = lwd))

【讨论】:

  • ggplot2 的另一种选择是重叠两层:一层用直径增加的填充点,另一层在顶部用较小的点和灰色填充。
  • 这只会改变点的粗细,而不是图例中的大小。我怎样才能增加后者以匹配前者?
  • @wdkrnls,抱歉耽搁了。我能做的最好的就是深入研究情节 grob 结构以更改图例键以匹配点。查看编辑。
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
相关资源
最近更新 更多