facet_wrap ggplot2 中的标签 hline
Label hline in facetwrap ggplot2
我正在尝试在我的 ggplot 中标记 hline,每个 facet.The facet_wrap 显示多个 Ratings
的值,并且hline 值来自 Benchmark
。
我试过在 geom_hline 中使用 label=
但这不起作用。我将不胜感激有关如何解决此问题的任何帮助 and/or 的建议。
下面是一个示例代码来说明我的观点。先感谢您。
Rating = as.factor(c("A partner", "Approachable", "Arrogant", "Exciting"))
A1 = as.integer(c(38,53,42,40))
A2 = as.integer(c(40,41,55,35))
A3 = as.integer(c(43,57,11,23))
A4 = as.integer(c(56,36,48,50))
Benchmark = as.integer(c(50,20,64,43))
df_full <- data.frame(Rating, A1, A2, A3, A4, Benchmark)
df <- df_full[,c(1:5)]
library(reshape2)
df_long <- melt(df, id = "Rating")
library(ggplot2)
p<-ggplot(data=df_long, aes(x=variable, y=value, fill = variable, label = value)) +
#Order ratings
facet_wrap(factor(Rating, levels = c( "A partner"
,"Approachable"
,"Exciting"
,"Arrogant"))~., scales = "free") +
geom_bar(stat="identity") +
geom_text (aes()
, hjust = -0.5
, size = 5
,position = position_dodge(width = -1)) +
geom_hline(data = df_full
, aes(yintercept = as.integer(Benchmark))
, color = "black"
, linetype="dashed") +
coord_flip() +
ylim(0, 100)
p
也许第二个 geom_text
:
# Using OPs provided data
library(ggplot2)
Rating <- factor(Rating, Rating)
ggplot(df_long, aes(variable, value, label = value)) +
geom_bar(aes(fill = variable), stat = "identity") +
geom_text(hjust = -0.5, position = position_dodge(width = -1), size = 5) +
geom_text(data = df_full, aes(1, Benchmark, label = Benchmark),
vjust = 2, hjust = -0.2) +
geom_hline(data = df_full, aes(yintercept = Benchmark),
linetype = "dashed") +
facet_wrap(~ Rating, scales = "free") +
coord_flip(ylim = c(0, 100))
我正在尝试在我的 ggplot 中标记 hline,每个 facet.The facet_wrap 显示多个 Ratings
的值,并且hline 值来自 Benchmark
。
我试过在 geom_hline 中使用 label=
但这不起作用。我将不胜感激有关如何解决此问题的任何帮助 and/or 的建议。
下面是一个示例代码来说明我的观点。先感谢您。
Rating = as.factor(c("A partner", "Approachable", "Arrogant", "Exciting"))
A1 = as.integer(c(38,53,42,40))
A2 = as.integer(c(40,41,55,35))
A3 = as.integer(c(43,57,11,23))
A4 = as.integer(c(56,36,48,50))
Benchmark = as.integer(c(50,20,64,43))
df_full <- data.frame(Rating, A1, A2, A3, A4, Benchmark)
df <- df_full[,c(1:5)]
library(reshape2)
df_long <- melt(df, id = "Rating")
library(ggplot2)
p<-ggplot(data=df_long, aes(x=variable, y=value, fill = variable, label = value)) +
#Order ratings
facet_wrap(factor(Rating, levels = c( "A partner"
,"Approachable"
,"Exciting"
,"Arrogant"))~., scales = "free") +
geom_bar(stat="identity") +
geom_text (aes()
, hjust = -0.5
, size = 5
,position = position_dodge(width = -1)) +
geom_hline(data = df_full
, aes(yintercept = as.integer(Benchmark))
, color = "black"
, linetype="dashed") +
coord_flip() +
ylim(0, 100)
p
也许第二个 geom_text
:
# Using OPs provided data
library(ggplot2)
Rating <- factor(Rating, Rating)
ggplot(df_long, aes(variable, value, label = value)) +
geom_bar(aes(fill = variable), stat = "identity") +
geom_text(hjust = -0.5, position = position_dodge(width = -1), size = 5) +
geom_text(data = df_full, aes(1, Benchmark, label = Benchmark),
vjust = 2, hjust = -0.2) +
geom_hline(data = df_full, aes(yintercept = Benchmark),
linetype = "dashed") +
facet_wrap(~ Rating, scales = "free") +
coord_flip(ylim = c(0, 100))