首页 文章

在R中的堆积条形图上绘制散点图

提问于
浏览
2

我正在尝试将散点图(带有误差条)叠加在R中的堆积条形图上,该条形图用作热图样式回填,以直观地显示这些点属于哪些类别 . 我已成功构建了两个组件,但似乎无法成功组合它们,或者找到一个类似的示例来绘制 .

编辑添加:在将代码剥离到两个图形的ggplot()geom结构并删除误差条后,我可以在同一个图形上获得两个,但是比例关闭(尽管两者都有ymax = 35)和我无法让它们重叠 .

##library/packages

library(reshape2)
library(ggplot2)
library(forcats) #forcats package
library(scales)
library(plyr)
library(ggplot2)

#Data for point graph:
df<-data.frame(Location=c("Location1","Location2", "Location3"), WALL=c(3.5,1.6,30), NRPK=c(5.6,1.0,21), WALL_CL_L=c(3.2,1.5,27),
               WALL_CL_U=c(3.8,2.0,32), NRPK_CL_L=c(5.0,0.05,19.3), NRPK_CL_U=c(6.1,1.2,23.5))
xWALL<-subset(df, select=c("Location","WALL","WALL_CL_L","WALL_CL_U"))

#Data for bar graph:
dat <- read.table(text = "     FSI_Scale
                  1   6
                  2   9
                  3   7
                  4   8
                  5   5",sep = "",header = TRUE)

datm <- melt(cbind(dat, ind = rownames(dat)), id.vars = c('ind'))

MyColours<-c('green3','green2','yellow1','orange1','red')

Basic<- ggplot() +
  geom_point(data=xWALL, aes(x=Location, y=WALL), size=2, shape=23, color="black", fill="cornflowerblue") +
  geom_bar(data=datm, aes(x=variable, y=value, fill=forcats::fct_rev(ind)), stat = "identity", position = "fill", width = 1) + scale_fill_manual(values = MyColours)+
  theme(axis.title=element_blank(), axis.text=element_blank(),axis.ticks=element_blank())+
  guides(fill=FALSE)+
  scale_y_continuous(limits=c(0,35))

结果如下:barandpoint

非常感谢您提供的任何帮助 .

2 回答

  • 0

    您收到错误消息 ggplot2 doesn't know how to deal with data of class uneval ,因为您尝试组合绘图而不首先定义数据的来源 . 这是使用您的代码绘制它的正确方法:

    p<- ggplot() +
      geom_point(data=xWALL, aes(x=Location, y=WALL), size=2, shape=23, color="black", fill=Loccolors) +
      geom_errorbar(data=xWALL, aes(ymax=WALL_CL_U, ymin=WALL_CL_L), width=0.05, size=0.1) +
      scale_y_continuous(limits=c(0,35),expand=c(0,0))+
      theme_bw()+
      theme_classic()+
      axis.line = element_line(color = 'black')+
      geom_bar(data = datm, aes(x=variable, y=value, fill=forcats::fct_rev(ind)), position = "fill",stat = "identity", width = 1) + scale_fill_manual(values = MyColours)+
      theme(axis.title=element_blank(), axis.text=element_blank(),axis.ticks=element_blank())+
      guides(fill=FALSE)+
      scale_y_continuous(labels = percent_format())
    

    这应该有效,但因为你真的懒得经历代码 . 尝试删除任何不必要的代码,例如将数据帧打印到控制台或绘图时,删除 theme_bw()+ theme_classic() ,因为这并不重要 .

  • 0

    我最后回答了自己的问题 . 必要的步骤是制作一个非常基本的热图(而不是条形图)并叠加点:

    library(RColorBrewer)
    library(ggplot2)
    #heat map dataframe
    d<-data.frame(x=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2), y=rep(0:25,2), z=c(1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5), w=rep(2,52))
    #data point dataframe
    pd<-data.frame(Loc=c(1,2), var=c(11,17))
    #colour palette for heat map
    colfunc<-colorRampPalette(c("red","darkorange","yellow1","springgreen","springgreen3"))
    
    ggplot() +
      geom_tile(data=d, aes(x,y, fill = z), show.legend=FALSE)  +
      scale_fill_gradientn(colours = colfunc(5)) +
      scale_x_continuous(expand = c(0, 0)) + 
      scale_y_continuous(breaks=pretty(d$y, n=10),expand = c(0, 0)) + #point after heatmap or it will be covered
      geom_point(data=pd,aes(Loc, var))+
      theme_bw() +
      theme(axis.title.x =element_blank(),
            axis.title.y=element_blank(),
            panel.grid.major = element_blank(), 
            panel.grid.minor = element_blank(), 
            panel.border = element_blank(),
            panel.background = element_blank())
    

    这允许根据需要调整热图以显示不同的阈值,并显示数据相对于它们的位置 .

相关问题