使用ggplot在同一图表中绘制箱线图和线图

时间:2015-07-23 17:49:35

标签: r plot ggplot2

我想在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))

enter image description here

问题在于,对于箱形图或小提琴图,我需要使用x轴作为因子。另一方面,我需要x轴是一个数字。我基本上想要拟合一个理论线,我生成的箱形图。我怎么可能这样做?

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

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)

Yeilds:

enter image description here

测试数据:

:root, html {
  overflow-x: auto;
  overflow-y: scroll;
}

body {
  margin: 0;
  padding: 0;
}

.thing {
  width: 100vw;
  height: 110vh;
  background: blue;
}

This was helpful here.

相关问题