Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将箱线图与ggplot2中的直方图叠加_R_Ggplot2 - Fatal编程技术网

R 将箱线图与ggplot2中的直方图叠加

R 将箱线图与ggplot2中的直方图叠加,r,ggplot2,R,Ggplot2,嗨,我想用R脚本创建一个类似的图表,如下所示: 摘自: 这是我在R中的代码: library(ggplot2) ifile <- read.table("C:/ifiles/test.txt", skip = 2, header = TRUE, sep="\t") ifileVI <- data.frame(ifile["VI"], ifile["Site"]) x<-quantile(ifileVI$VI,c(0.01,0.99)) data_clean <- if

嗨,我想用R脚本创建一个类似的图表,如下所示:

摘自:

这是我在R中的代码:

library(ggplot2)

ifile <- read.table("C:/ifiles/test.txt", skip = 2, header = TRUE, sep="\t")
ifileVI <- data.frame(ifile["VI"], ifile["Site"])
x<-quantile(ifileVI$VI,c(0.01,0.99))
data_clean <- ifileVI[bfileVI$VI >=x[1] & ifileVI$VI <=x[2],]

p <- ggplot(data_clean, aes(x = Site, y = VI, group=Site)) + geom_boxplot() + geom_histogram(binwidth = 0.05)

p
bfileVI:
Id VI站点
WFR1 2.91 1
WFR1 2.89 2
WFR1 2.86 3
WFR1 2.91 4
WFR1 2.87 1
WFR1 2.67 2
WFR1 2.76 3
WFR1 2.74 4
WFR1 2.98 4
WFR1 2.89 3
WFR1 2.55 4
WFR1 2.96 3
WFR1 2.71 1
WFR1 2.98 2
WFR1 2.89 3
WFR2 2.55 2
WFR2 2.86 4
WFR2 2.91 3
WFR2 287 1
WFR2 2.74 2
WFR2 2.98 1
WFR2 2.89 2
WFR2 2.55 3
WFR2 2.96 4
WFR2 2.71 1
WFR2 2.86 2
WFR2 2.91 3
WFR2 287 4
WFR2 2.67 1
WFR2 2.76 2
WFR2 2.74 3
WFR2 2.98 4
WFR2 2.89 1
WFR2 2.55 2
WFR2 2.96 3
WFR2 2.71 4
WFR2 2.98 1
WFR2 2.89 2
WFR2 2.55 3

WFR2 2.86 4
您得到了
错误:stat_bin()不能与y美学一起使用。
因为您不能在直方图的美学中指定
y
。如果要混合具有不同参数的绘图,则需要提供不同的美学效果。我将用
iris
这样演示:

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.05) +
  geom_boxplot(aes(x = 3, y = Sepal.Width))
不幸的是,箱线图的默认值是垂直的,直方图是水平的,
coord\u flip()
是全有或全无,所以你只剩下这个可怕的东西:

我能想到的最好办法是,不要让它们重叠,而是用
gridExtra
包将一个放在另一个上面:

a <- ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.05) 

b <- ggplot(iris, aes(x = "", y = Sepal.Width)) + 
  geom_boxplot() + 
  coord_flip()

grid.arrange(a,b,nrow=2)

a您可以尝试用矩形替换直方图,以生成如下图:


如何做到这一点: 生成随机数据 添加箱线图 要添加箱线图,我们需要:

  • 翻转坐标(函数
    coord\u Flip
  • geom\u rect
  • 代码:

    结果:

    具有更好视觉效果的最终绘图代码
    一个很好的例子是,
    ggstance
    包很方便

    库(ggplot2)
    图书馆(GGSTANTE)
    ggplot(鸢尾,aes(x=萼片长度))+
    geom_直方图()+
    几何图形(aes(y=3),宽度=2,颜色=“蓝色”,lwd=2,α=0.5)+
    主题_极小值()+
    镶嵌面(物种,ncol=1)
    

    请使用
    dput()
    共享您的数据这可能是的重复--不会将图表放在彼此的顶部,但我认为像示例中那样重叠它们不是一个好主意。。。淹没了大脑。上面的链接@神秘提供了更多细节,例如如何匹配当前在绘图中未对齐的轴范围(例如顶部绘图中的
    2.0
    与底部绘图中的
    2.0
    不对齐)。您好,我已经在blufileVf2中显示了我的样本数据(在我的回答中)如何调整脚本以处理该文件?你的Y是我的Vf2箱,X是频率?有4个独立的“站点”1、2、3,4@Adhil添加更多数据并使用dput功能(不是图片)抱歉,我不懂,我是新来的r:)@Adhil将数据粘贴到问题(不是图片,没有任何用处)我已粘贴了数据
    a <- ggplot(iris, aes(x = Sepal.Width)) + 
      geom_histogram(binwidth = 0.05) 
    
    b <- ggplot(iris, aes(x = "", y = Sepal.Width)) + 
      geom_boxplot() + 
      coord_flip()
    
    grid.arrange(a,b,nrow=2)
    
    df <- data.frame(State = LETTERS[1:3],
                     Y = sample(1:10, 30, replace = TRUE),
                     X = rep(1:10, 3))
    
    library(ggplot2)
    
    # You can plot geom_histogram or bar (pre-counted stats)
    ggplot(df, aes(X, Y)) +
        geom_bar(stat = "identity", position = "dodge") +
        facet_grid(State ~ .)
    # Or you can plot similar figure with geom_rect
    ggplot(df)  +
        geom_rect(aes(xmin = X - 0.4, xmax = X + 0.4, ymin = 0, ymax = Y)) +
        facet_grid(State ~ .)
    
    ggplot(df)  +
        geom_rect(aes(xmin = 0, xmax = Y, ymin = X - 0.4, ymax = X + 0.4)) +
        geom_boxplot(aes(X, Y)) +
        coord_flip() +
        facet_grid(State ~ .)
    
    ggplot(df)  +
        geom_rect(aes(xmin = 0, xmax = Y, ymin = X - 0.4, ymax = X + 0.4),
                  fill = "blue", color = "black") +
        geom_boxplot(aes(X, Y), alpha = 0.7, fill = "salmon2") +
        coord_flip() +
        facet_grid(State ~ .) +
        theme_classic() +
        scale_y_continuous(breaks = 1:max(df$X))