我想在形状圆圈中使用不同程度的填充来表示数据集中的不同变量。我的数据集中有一列名为“形状”的列,其中的数字 0、1、2、3 对应于我想在图表中描绘的某些变量组合。我想要:
我可以使用 R 中内置的形状代码设置 4 种不同的形状,但不幸的是我找不到半实心圆的任何代码。因此,我尝试构建自己的形状,但是我似乎无法
ggplot2
识别它们,它们要么只是显示为正方形,要么我收到删除这些行的警告消息。
# 4 different shapes using shape values from R ###
# define dodge position
pd <- position_dodge(.6)
newdata_small %%
ggplot(aes(x=as.numeric(F6_GR),
y=emmean,
shape=shape)) + # shape = condition
geom_errorbar(aes(ymin=emmean-SE,
ymax=emmean+SE), width=.2, position = pd) +
geom_line(position = pd)+
geom_point(position = pd, size=5) +
theme_classic() +
ggtitle("Small Populations \n")+
scale_shape_manual(name= "Heat Waves",
labels=c("No heat waves", "Initial heat wave only",
"Final heat wave only ", "Initial & final heat wave"),
values = c(1, 5, 6, 19))
# Looks good but I want half filled circles.
然后我尝试使用以下代码创建自己的自定义形状:
library(grid)
# Define custom point shapes
half_filled_left <- function(size) {
polygonGrob(x = c(-0.5, 0.5, 0.5, -0.5),
y = c(-0.5, -0.5, 0.5, 0.5),
gp = gpar(col = "black", fill = "yellow"))
arc <- seq(0, pi, length.out = 100)
linesGrob(x = cos(arc) * 0.5,
y = sin(arc) * 0.5,
gp = gpar(col = "black", lwd = size / 5))
}
half_filled_right <- function(size) {
polygonGrob(x = c(-0.5, 0.5, 0.5, -0.5),
y = c(-0.5, -0.5, 0.5, 0.5),
gp = gpar(col = "black", fill = "red"))
arc <- seq(pi, 2*pi, length.out = 100)
linesGrob(x = cos(arc) * 0.5,
y = sin(arc) * 0.5,
gp = gpar(col = "black", lwd = size / 5))
}
这就是我尝试添加到我的情节中的方式:
newdata_small %>%
ggplot(aes(x=as.numeric(F6_GR),
y=emmean,
shape=shape)) + #shape = condition
geom_errorbar(aes(ymin=emmean-SE,
ymax=emmean+SE),
width=.2, position = pd) +
geom_line(position = pd)+
geom_point(position = pd, size=5) +
theme_classic() +
ggtitle("Small Populations \n")+
scale_shape_manual(name= "Heat Waves",
labels=c("No heat waves", "Initial heat wave only",
"Final heat wave only ", "Initial & final heat wave"),
values = c("0" = 1, # Circle outline only
"1" = half_filled_left(5), # Half filled circle left side
"2" = half_filled_right(5), # Half filled circle right side
"3" = 16)) # Default filled circle
但它只是返回一条错误消息,指出它已删除了我想要描绘为自定义形状的 4 行。在我的代码中使用这些自定义形状的最佳方法是什么,或者有没有办法指定半实心圆?
实现所需结果的一个轻量级选项是使用 unicode Shapes,即
"\u25d6"
和 "\u25d7"
会给你一个左/右半圆。但请注意,并非所有字体都支持这些符号。此外,您不能将 unicode 与默认形状混合,因此您也必须对其他形状使用 unicode 符号:
library(ggplot2)
pd <- position_dodge(.6)
newdata_small <- data.frame(
F6_GR = factor(rep(LETTERS[1:2], 4)),
shape = as.character(rep(0:3, each = 2)),
emmean = 1:8,
SE = .25
)
newdata_small |>
ggplot(aes(
x = as.numeric(F6_GR),
y = emmean,
shape = shape
)) +
geom_errorbar(aes(
ymin = emmean - SE,
ymax = emmean + SE
), width = .2, position = pd) +
geom_line(position = pd) +
geom_point(position = pd, size = 6) +
scale_shape_manual(
name = "Heat Waves",
labels = c(
"No heat waves", "Initial heat wave only",
"Final heat wave only ", "Initial & final heat wave"
),
values = c("\u25cb", "\u25d6", "\u25d7", "\u25cf")
) +
theme_classic() +
theme(plot.title = element_text(
margin = margin(b = 20)
)) +
ggtitle("Small Populations")