为笔画点添加图例 ggplot2
Add legend for stroke points ggplot2
我想为图中的笔划粗细添加一个图例。
你知道我该怎么做吗?
我可以为尺寸添加图例,但不能为笔划添加图例。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") +
scale_size(range=c(2,12), breaks=c(0,01,02),
labels=c(">=0",">=0.1",">=0.2"), guide="legend")
我认为没有开箱即用的笔划缩放功能,但您当然可以构建自己的缩放功能。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") +
scale_size(range=c(2,12), breaks=c(0,01,02),
labels=c(">=0",">=0.1",">=0.2"), guide="legend") +
continuous_scale("stroke", "stroke",
palette = function(x){scales::rescale(x, c(0, 6))},
breaks = c(0, 2, 4, 6))
编辑:
我刚刚发现 scales::rescale_pal
,这可能比为连续规模制作一个临时匿名函数更优雅。
continuous_scale("stroke", "stroke",
palette = scales::rescale_pal(c(0, 6)),
breaks = c(0, 2, 4, 6))
@teunbrand 的回答肯定是更优雅的方式。
但是,当再次查看您的问题时,a 刚刚提出了另一种解决方法,即使用附加层创建另一个指南,然后手动调整该指南以重新采样笔划宽度。
请注意,您可以使用 theme(legend.key.size=unit(2, 'lines'))
使图例适合您的笔画大小。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(aes(x=size, y=stroke, size = size, stroke = stroke, color=factor(stroke)), data=sizes) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") + # to create one layer that builds a legend for 'color'
geom_point(shape = 21, fill = "red", color='black') +
theme(legend.key.size=unit(2, 'lines')) +
scale_size(range=c(2,12),breaks=c(0,1,2),labels=c(">=0",">=1",">=2"),guide='legend') +
guides(color=guide_legend(override.aes = list(stroke=c(0, 2, 4, 6), color='black'), title='stroke')) # fix the color guide to show stroke width
我想为图中的笔划粗细添加一个图例。 你知道我该怎么做吗? 我可以为尺寸添加图例,但不能为笔划添加图例。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") +
scale_size(range=c(2,12), breaks=c(0,01,02),
labels=c(">=0",">=0.1",">=0.2"), guide="legend")
我认为没有开箱即用的笔划缩放功能,但您当然可以构建自己的缩放功能。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") +
scale_size(range=c(2,12), breaks=c(0,01,02),
labels=c(">=0",">=0.1",">=0.2"), guide="legend") +
continuous_scale("stroke", "stroke",
palette = function(x){scales::rescale(x, c(0, 6))},
breaks = c(0, 2, 4, 6))
编辑:
我刚刚发现 scales::rescale_pal
,这可能比为连续规模制作一个临时匿名函数更优雅。
continuous_scale("stroke", "stroke",
palette = scales::rescale_pal(c(0, 6)),
breaks = c(0, 2, 4, 6))
@teunbrand 的回答肯定是更优雅的方式。
但是,当再次查看您的问题时,a 刚刚提出了另一种解决方法,即使用附加层创建另一个指南,然后手动调整该指南以重新采样笔划宽度。
请注意,您可以使用 theme(legend.key.size=unit(2, 'lines'))
使图例适合您的笔画大小。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(aes(x=size, y=stroke, size = size, stroke = stroke, color=factor(stroke)), data=sizes) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") + # to create one layer that builds a legend for 'color'
geom_point(shape = 21, fill = "red", color='black') +
theme(legend.key.size=unit(2, 'lines')) +
scale_size(range=c(2,12),breaks=c(0,1,2),labels=c(">=0",">=1",">=2"),guide='legend') +
guides(color=guide_legend(override.aes = list(stroke=c(0, 2, 4, 6), color='black'), title='stroke')) # fix the color guide to show stroke width