使用 ggplot(Box and Whisker 类型)在条形图顶部绘制线条
Drawing lines on top of bar chart using ggplot (Box and Whisker kind)
我正在尝试绘制以下内容:
在 ggplot 中给定一个与此类似的条形图:一个分组的条形图,在 x 轴上翻转。
我想在每个条形图的顶部画一条线。该线的经度在数据帧 df2 中定义。 (将其视为某种标准偏差)
线的经度已定义,它将以每个柱的最高点为中心。
如您所见,这与箱线图不同,它是一条始终保持不变并居中于每个条形图顶部的线。
所以我想知道是否可以使用我在下一节中展示的代码来做到这一点。在 excel 中,使用所谓的盒须图可以非常快地完成此操作。最终结果应如下所示:
这是我目前使用的代码,希望有人能帮帮我。
df <- read.table(
text =
"group metric somevalue
T1 epsilon 63
T2 epsilon 91
T1 kappa 19
T2 kappa -3
T1 zulu -5
T2 zulu 8", header=TRUE)
str(df)
df$metric <- factor(df$metric, levels = c("kappa", "zulu", "epsilon"))
df2 <- read.table(
text =
"group metric deviation
T1 epsilon 20
T2 epsilon 10
T1 kappa 10
T2 kappa 20
T1 zulu 25
T2 zulu 10", header=TRUE)
thePlot <- ggplot(data=df, aes(x=metric, y=somevalue, fill=group)) + geom_bar(position = 'dodge', stat='identity') + coord_flip() + theme(panel.background = element_blank(), legend.position="bottom",
legend.text=element_text(size=14),
legend.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())+
scale_fill_manual(values=c("#CC6600", "steelblue")) + ylim(-20, 100)
thePlot
更新:请注意,使用 geom_errorbar() 的内容必须使用不同的错误置信区间,每个置信区间实际上都在 df2 数据框中。所以这就像一个动态绘图,在条形图上逐条绘制。
您可能想使用 'geom_errorbar',它会在调用 'coord_flip' 时翻转。
这里有一个来自我自己数据的例子:
library(ggplot2)
library(Rmisc)
group_data <- summarySE(data = Data_P_L, measurevar = "w", groupvars = c("Median_Age_Split","Stake"))
ggplot(data = group_data, aes(x = Median_Age_Split, color = Stake, fill = Stake)) +
geom_bar(stat = "identity", aes(x = Median_Age_Split, y = w),
position= "dodge", color = "black", alpha = 1) +
geom_errorbar(aes(x = Median_Age_Split, ymin=w-ci, ymax=w+ci), position=position_dodge(.9),
width = 0.2, size = 1, color = "black") +
theme_light()
geom_bar(stat = "identity", aes(x = Median_Age_Split, y = w),
position= "dodge", color = "black", alpha = 1) +
geom_errorbar(aes(x = Median_Age_Split, ymin=w-ci, ymax=w+ci), position=position_dodge(.9),
width = 0.2, size = 1, color = "black") +
coord_flip() +
theme_light()
我正在尝试绘制以下内容: 在 ggplot 中给定一个与此类似的条形图:一个分组的条形图,在 x 轴上翻转。
我想在每个条形图的顶部画一条线。该线的经度在数据帧 df2 中定义。 (将其视为某种标准偏差)
线的经度已定义,它将以每个柱的最高点为中心。 如您所见,这与箱线图不同,它是一条始终保持不变并居中于每个条形图顶部的线。
所以我想知道是否可以使用我在下一节中展示的代码来做到这一点。在 excel 中,使用所谓的盒须图可以非常快地完成此操作。最终结果应如下所示:
这是我目前使用的代码,希望有人能帮帮我。
df <- read.table(
text =
"group metric somevalue
T1 epsilon 63
T2 epsilon 91
T1 kappa 19
T2 kappa -3
T1 zulu -5
T2 zulu 8", header=TRUE)
str(df)
df$metric <- factor(df$metric, levels = c("kappa", "zulu", "epsilon"))
df2 <- read.table(
text =
"group metric deviation
T1 epsilon 20
T2 epsilon 10
T1 kappa 10
T2 kappa 20
T1 zulu 25
T2 zulu 10", header=TRUE)
thePlot <- ggplot(data=df, aes(x=metric, y=somevalue, fill=group)) + geom_bar(position = 'dodge', stat='identity') + coord_flip() + theme(panel.background = element_blank(), legend.position="bottom",
legend.text=element_text(size=14),
legend.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())+
scale_fill_manual(values=c("#CC6600", "steelblue")) + ylim(-20, 100)
thePlot
更新:请注意,使用 geom_errorbar() 的内容必须使用不同的错误置信区间,每个置信区间实际上都在 df2 数据框中。所以这就像一个动态绘图,在条形图上逐条绘制。
您可能想使用 'geom_errorbar',它会在调用 'coord_flip' 时翻转。
这里有一个来自我自己数据的例子:
library(ggplot2)
library(Rmisc)
group_data <- summarySE(data = Data_P_L, measurevar = "w", groupvars = c("Median_Age_Split","Stake"))
ggplot(data = group_data, aes(x = Median_Age_Split, color = Stake, fill = Stake)) +
geom_bar(stat = "identity", aes(x = Median_Age_Split, y = w),
position= "dodge", color = "black", alpha = 1) +
geom_errorbar(aes(x = Median_Age_Split, ymin=w-ci, ymax=w+ci), position=position_dodge(.9),
width = 0.2, size = 1, color = "black") +
theme_light()
geom_bar(stat = "identity", aes(x = Median_Age_Split, y = w),
position= "dodge", color = "black", alpha = 1) +
geom_errorbar(aes(x = Median_Age_Split, ymin=w-ci, ymax=w+ci), position=position_dodge(.9),
width = 0.2, size = 1, color = "black") +
coord_flip() +
theme_light()