ggplot2:两个数值变量的 geom_boxplot 表示形式存在问题
ggplot2: Problem with geom_boxplot representation for two numerical variables
我想在箱线图(上四分位数和下四分位数、胡须)中进行 error
表示,但没有成功。我有两个探索性变量(distance
和 radius
),如果我用误差线绘制均值图是可以的,但如果我尝试在箱线图中绘制相同的图,我没有每个距离 5 个半径。在我的例子中:
# Open my ds
sim_F<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/rad_dist_prob.csv")
# Aggregate mean
df_err<-sim_F%>%
group_by(distance,radius) %>%
summarize(error = mean(error, na.rm = TRUE)*100)
df_err
# Aggregate standart error
df_sd_err<-sim_F%>%
group_by(distance, radius) %>%
summarize(sd = sd(error, na.rm = TRUE)/sqrt(998)*100)
df_sd_err
#
# The errorbars overlapped, so use position_dodge to move them horizontally
pd <- position_dodge(0.1)
# First create a mean +-SD error bar
ggplot(data=df_err, aes(x=distance, y=error, color=as.factor(radius))) +
geom_errorbar(data=df_err,mapping=aes(ymin=error-df_sd_err$sd, ymax=error+df_sd_err$sd),position=pd, width=10) +
scale_x_continuous(breaks=seq(20, 100, by = 5)) +
xlab ("Distance (m)") +
ylab ("Error (%)")
# Create same pattern but in a boxplot
ggplot(data=df_err, aes(x=distance, y=error, color=as.factor(radius))) +
geom_boxplot() +
xlab ("Distance (m)") +
ylab ("Error (%)")
#
# I don't have de 5 radius in each distance
# I try to:
ggplot(data=df_err, aes(x=as.factor(distance), y=error, color=as.factor(radius))) +
geom_boxplot() +
xlab ("Distance (m)") +
ylab ("Error (%)")
# Doesn't work too!!
拜托,对于每个 distance
的 5 radius
的箱线图表示有什么想法,就像我的第一个带有误差线的图一样?我不喜欢使用 facet_wrap
.
在情节中进行细分
问题似乎是,在使用 summarise
聚合数据后,distance/radius 的每个 group/subgroup 只有一个数据,盒须图不再有意义.
下面的代码不聚合数据,它只是将分组变量转换为因子并将它们绘制在 x 轴上。 y 轴百分比刻度标签使用包 scales
、函数 label_percent
.
library(dplyr)
library(ggplot2)
sim_F %>%
mutate(distance = factor(distance),
radius = factor(radius)) %>%
ggplot(aes(x = distance, y = error, color = radius)) +
geom_boxplot() +
scale_y_continuous(labels = scales::label_percent()) +
xlab ("Distance (m)") +
ylab ("Error (%)")
数据
URL <- "https://raw.githubusercontent.com/Leprechault/trash/main/rad_dist_prob.csv"
download.file(url = URL, destfile = "rad_dist_prob.csv")
sim_F <- read.csv("rad_dist_prob.csv")
我想在箱线图(上四分位数和下四分位数、胡须)中进行 error
表示,但没有成功。我有两个探索性变量(distance
和 radius
),如果我用误差线绘制均值图是可以的,但如果我尝试在箱线图中绘制相同的图,我没有每个距离 5 个半径。在我的例子中:
# Open my ds
sim_F<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/rad_dist_prob.csv")
# Aggregate mean
df_err<-sim_F%>%
group_by(distance,radius) %>%
summarize(error = mean(error, na.rm = TRUE)*100)
df_err
# Aggregate standart error
df_sd_err<-sim_F%>%
group_by(distance, radius) %>%
summarize(sd = sd(error, na.rm = TRUE)/sqrt(998)*100)
df_sd_err
#
# The errorbars overlapped, so use position_dodge to move them horizontally
pd <- position_dodge(0.1)
# First create a mean +-SD error bar
ggplot(data=df_err, aes(x=distance, y=error, color=as.factor(radius))) +
geom_errorbar(data=df_err,mapping=aes(ymin=error-df_sd_err$sd, ymax=error+df_sd_err$sd),position=pd, width=10) +
scale_x_continuous(breaks=seq(20, 100, by = 5)) +
xlab ("Distance (m)") +
ylab ("Error (%)")
# Create same pattern but in a boxplot
ggplot(data=df_err, aes(x=distance, y=error, color=as.factor(radius))) +
geom_boxplot() +
xlab ("Distance (m)") +
ylab ("Error (%)")
# I don't have de 5 radius in each distance
# I try to:
ggplot(data=df_err, aes(x=as.factor(distance), y=error, color=as.factor(radius))) +
geom_boxplot() +
xlab ("Distance (m)") +
ylab ("Error (%)")
# Doesn't work too!!
拜托,对于每个 distance
的 5 radius
的箱线图表示有什么想法,就像我的第一个带有误差线的图一样?我不喜欢使用 facet_wrap
.
问题似乎是,在使用 summarise
聚合数据后,distance/radius 的每个 group/subgroup 只有一个数据,盒须图不再有意义.
下面的代码不聚合数据,它只是将分组变量转换为因子并将它们绘制在 x 轴上。 y 轴百分比刻度标签使用包 scales
、函数 label_percent
.
library(dplyr)
library(ggplot2)
sim_F %>%
mutate(distance = factor(distance),
radius = factor(radius)) %>%
ggplot(aes(x = distance, y = error, color = radius)) +
geom_boxplot() +
scale_y_continuous(labels = scales::label_percent()) +
xlab ("Distance (m)") +
ylab ("Error (%)")
数据
URL <- "https://raw.githubusercontent.com/Leprechault/trash/main/rad_dist_prob.csv"
download.file(url = URL, destfile = "rad_dist_prob.csv")
sim_F <- read.csv("rad_dist_prob.csv")