[英]plotting a boxplot and a line plot in the same graph using ggplot
我想在ggplot中為同一圖中的箱形圖和線形圖創建圖。 我有一個數據框架,如下所示:
Lambda | means | theorMean
1 0.1 10.07989 10
2 0.1 10.55681 10
3 0.1 10.26660 10
4 0.1 10.29234 10
5 0.1 10.07754 10
...
means
是樣本均值,理論均值是theorMeans
。 我想通過箱形圖來繪制樣本均值的分布,而理論上的意思是使用直線。
這是我到目前為止所擁有的...
library(ggplot2)
library(scales)
p <- ggplot(summ, aes(x=factor(Lambda), y=means)) +
geom_boxplot() +
geom_line(data=summ, aes(x=log10(Lambda), y=means))
問題是,對於箱形圖或小提琴圖,我需要使用x軸作為因子。 另一方面,我需要x軸為數字。 我基本上想使理論線適合我生成的箱形圖。 我該怎么辦?
這應該可以解決問題:
library(ggplot2)
summ$Lambda <- log10(summ$Lambda)
ggplot(summ, aes(x=factor(Lambda), y=means)) +
geom_boxplot() +
geom_line(inheret.aes = FALSE, aes(x=factor(Lambda), y=means, group = 1), color = "blue") +
ylab("Mean") +
xlab("Lambda (Log10)") +
scale_x_discrete(labels = round(summ$Lambda,2)) +
theme( axis.ticks.y = element_blank()
, axis.text.x = element_text(angle = 45, hjust = 1)
)
良率:
測試數據:
e <- 2.7182818284590452353602874713527
summ <- data.frame("Lambda" = seq(0.01, 0.9, by = 0.0287097))
list <- c()
for(x in 1:31){
t <- e^-(x/10)*15
list[x] <- t
}
summ$means <- list
summ$Lambda <- log10(summ$Lambda)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.