R 如何更改ggplot中的点形状
我试图将ggplot中的点形状更改为不同的形状,而不是统一的圆形,但保持不同的颜色,我认为它比圆形更好的视觉标识符,我尝试在geom_point中使用:R 如何更改ggplot中的点形状,r,ggplot2,R,Ggplot2,我试图将ggplot中的点形状更改为不同的形状,而不是统一的圆形,但保持不同的颜色,我认为它比圆形更好的视觉标识符,我尝试在geom_point中使用: aes(shape=c(0,1,2,8)) 我得到一个错误: Error: A continuous variable can not be mapped to shape 这是我的代码: library(repr, warn.conflicts = FALSE) options(repr.plot.width=3.5, repr.plot.
aes(shape=c(0,1,2,8))
我得到一个错误:
Error: A continuous variable can not be mapped to shape
这是我的代码:
library(repr, warn.conflicts = FALSE)
options(repr.plot.width=3.5, repr.plot.height=8.5)
zmapp072 <- ggplot(plot_frame, aes(Var2, Var1, fill = value)) +
geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +
geom_point(data = data.frame(Var2 = 1:4, Var1 = -1, value = 0), size = 5,
aes(color = factor(Var2))) +
geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
labels = c(expression(
CD44^{lo}~"T Cells",
CD44^{hi}~CD69^{lo}~"T Cells",
CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
guide = guide_legend(override.aes = list(fill = NA),
label.hjust = 0, position="bottom", size = 5)) +
scale_y_discrete(position = "right") +
labs(y = "", fill = "", color = " ", x = "") +
scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29"),
breaks = c(1.5, 0, -1.5),
labels = c("1.0", "0", "-1.0"),
limits = c(-1.5, 1.5),
space = "Lab",
guide = "colourbar",
aesthetics = "fill") +
theme_minimal() + guides(colour=FALSE) +
theme (panel.grid = element_blank(),
axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
axis.text.y = element_text(face="italic", size=7,
color="black"),
legend.justification = c(-0.9, 0),
legend.direction = "vertical",
legend.key.size = unit(0.6, "cm"),
legend.key.width = unit(0.2,"cm"),
legend.title.align = 0.5,
axis.text.x = element_blank(),plot.margin = unit(c(1,1,5,1), "lines")) +
guides(
fill = guide_colourbar(
title = "Relative gene expression \n (z score)",
title.position = "right",
title.theme = element_text(angle = -90, size = 7.5),
direction = "vertical",
ticks = FALSE)) +
coord_cartesian(clip = 'off')
legend25 <- ggplot(plot_frame, aes(Var2, Var1)) +
geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 5,
aes(color = factor(Var2))) +
geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
labels = c(expression(
CD44^{lo}~"T Cells",
CD44^{hi}~CD69^{lo}~"T Cells",
CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
guide = guide_legend(override.aes = list(size = 3),
label.hjust = 0)) +
theme (panel.grid = element_blank(),
axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
axis.text.y = element_text(face="italic", size=5,
color="black"),
legend.justification = c(-0.5, 0),
legend.direction = "vertical",
legend.key = element_blank(),
legend.title = element_blank(),
axis.text.x = element_blank())
legend <- cowplot::get_legend(legend25)
zmapp072 + annotation_custom(legend$grobs[[1]], xmin = 6, ymax = -13)
我还试图将同一行仅放在图例代码中,但不起作用,我搜索了同一行,发现了相同的错误,但似乎与我的代码无关。您的代码有很多问题。这是一个使用您的数据的最小解决方案。要控制形状,需要将其映射到
aes
内部的变量(需要是因子变量),或在aes
外部设置形状。后一种方法为整个绘图提供一个形状。您尝试的是为形状美学提供一个形状向量,1)期望一个长度等于其他变量的变量,2)期望一个因子,而不是一个连续(数字)变量。要将形状向量提供给形状比例
ggplot(plot_frame) +
geom_point(aes(x = value, y = Var1, shape = factor(Var2)))
方法1:将形状映射到变量并设置形状比例
ggplot(plot_frame) +
geom_point(aes(x = value, y = Var1, shape = factor(Var2)))
您可以控制哪些形状与scale\u shape\u manual()一起使用。
:
您还可以设置aes外部的形状,以便对所有点使用一个形状(默认形状除外)
这是一个很好的介绍性解决方案@BenNorris,很抱歉我的代码,我还是一个新手,在执行您的建议时,我丢失了图例文本,出现了另一个图例,颜色也没有指定,我试图将其添加到guide_legend中,但没有成功,我知道这里的颜色是多余的,但我想要一个混合色。
ggplot(plot_frame) +
geom_point(aes(x = value, y = Var1, shape = factor(Var2))) +
scale_shape_manual(values = c(0,1,2,8))
ggplot(plot_frame) +
geom_point(aes(x = value, y = Var1), shape = 8)