在现有的条形图和第二个y轴上添加一条线

时间:2019-04-18 13:35:14

标签: r ggplot2 yaxis

enter image description here enter image description here我是R的新手,而且堆栈溢出,所以请多多包涵。这里没有其他问题可以有效地解决我的问题。

我比较了四种靶向基因组检测一种标准品(AcroMetrix)中突变的能力。突变具有不同的类型和频率,因此我可以轻松地生成一个小图。 但是,该小组并未针对AcroMetrix中的所有突变。因此,我想添加一行目标百分比基准,并在右侧带有相应的y轴。

为清楚起见,请参见下文。

# This generates the barplot

df<-data.frame(row.names=c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"),Germline=c(34,33,14,22,12),Somatic_5_15=c(341,331,281,249,147),Somatic_15_30=c(180,176,129,124,108))

df$name<-row.names(df)  
df_molten<-melt(df)
df_molten$name <-factor(df_molten$name,
levels = c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"))

    ggplot(df_molten,aes(x=name,y=value,fill=variable))+
geom_bar(stat='identity')+
scale_fill_discrete(labels=c("Germline","Somatic 5-15% VAF","Somatic 15-30% VAF"))+
geom_text(aes(label=value),size=3,fontface='bold',position=position_stack(vjust=.5))+
xlab("Panel")+
ylab("Counts")+
theme_bw()+
theme(panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      panel.background=element_blank(),
      axis.line=element_line(colour="black"),
      panel.border=element_blank(),
      legend.title=element_blank())

# The second set of data for the percent targets are as follows, and this needs to form the line graph and be compared to the Y axis on the right:
 df1 <- data.frame(row.names=c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"),Percent_targeted=c(100,100,77,73,49))

1 个答案:

答案 0 :(得分:1)

修改3:

对不起,刚才看到了你的草图...

使用let mainDict:[String:Array] = ["MainDictionary": myArray]geom_point()创建线和点。向geom_line()添加一个数字(在本示例中为1),使点和线相对于条形移动。更改此设置,直到您拥有自己的职位为止。

使用Percent_targeted_scaled中的sizegeom_point()中的lwd创建合适的点大小和线宽。

geom_line()

enter image description here 编辑2:

要获取点(或标签)的百分比,请使用重新缩放的百分比值,如y美学:

library(ggplot2)
library(reshape2)
library(scales)
df<-data.frame(row.names=c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"),Germline=c(34,33,14,22,12),Somatic_5_15=c(341,331,281,249,147),Somatic_15_30=c(180,176,129,124,108))

df$name<-row.names(df)

df_molten<-melt(df)

df_molten$name<-factor(df_molten$name,levels=c('AcroMetrix','PV1_PV2','CHIPv2','TSACP','TSTP'))
df_molten$Percent_targeted <- unlist(lapply(1:length(levels(df_molten$variable)), function(i){c(100,100,77,73,49)}))
# counts <- df_molten %>% group_by(name) %>% summarise(sum=round(sum(value)))
# df_molten$Percent_targeted <- round(unlist(lapply(1:length(levels(df_molten$variable)), function(i){counts$sum/counts$sum[1]})), 2)*100

gg <- ggplot(df_molten,aes(x=name,y=value,fill=variable))+
  geom_bar(stat='identity', width=.6)+
  scale_fill_discrete(labels=c("Germline","Somatic 5-15% VAF","Somatic 15-30% VAF"))+
  geom_text(aes(label=value),size=3,fontface='bold',position=position_stack(vjust=.5))+
  xlab("Panel")+ylab("Counts")+
  theme_bw()+
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),panel.background=element_blank(),axis.line=element_line(colour="black"),panel.border=element_blank(),legend.title=element_blank())
gg <- gg + scale_y_continuous(expand = expand_scale(mult=c(0, 0.0)))


# get the sacle values of the current y-axis
gb <- ggplot_build(gg)
y.range <- gb$layout$panel_params[[1]]$y.range
y2.range <- range(df_molten$Percent_targeted)# extendrange(, f=0.01)
scale_factor <- (diff(y.range)/max(y2.range))
trans <- ~ ((. -y.range[1])/scale_factor)

df_molten$Percent_targeted_scaled <- rescale(df_molten$Percent_targeted, y.range, c(0, y2.range[2]))
df_molten$x <- which(levels(df_molten$name)%in%df_molten$name)#-.3

# gg <- gg + geom_segment(aes(x=x, xend=x, yend=Percent_targeted_scaled), y=0, size=2, data=df_molten)
# gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=Percent_targeted_scaled), fill='white', data=df_molten)
# gg <- gg + geom_hline(yintercept = y.range[2], linetype='longdash')
# gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=Percent_targeted_scaled), fill='white', data=df_molten, vjust=0)
gg <- gg + geom_point(aes(x=x, y=Percent_targeted_scaled+2), data=df_molten, show.legend = F, size=3)
gg <- gg + geom_line(aes(x=x, y=Percent_targeted_scaled+2), data=df_molten, lwd=1.5)
gg <- gg + scale_y_continuous(expand=expand_scale(mult=c(.05, .05)), sec.axis = sec_axis(trans, name = paste0("Percent genes targeted on ", levels(df_molten$name)[1]), labels = scales::percent(seq(0, 1, length.out = 5), scale=100)))
gg                

enter image description here 编辑:

我知道我们的目标是使水平线达到100%,这与library(ggplot2) library(reshape2) library(scales) df<-data.frame(row.names=c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"),Germline=c(34,33,14,22,12),Somatic_5_15=c(341,331,281,249,147),Somatic_15_30=c(180,176,129,124,108)) df$name<-row.names(df) df_molten<-melt(df) df_molten$name<-factor(df_molten$name,levels=c('AcroMetrix','PV1_PV2','CHIPv2','TSACP','TSTP')) df_molten$Percent_targeted <- unlist(lapply(1:length(levels(df_molten$variable)), function(i){c(100,100,77,73,49)})) gg <- ggplot(df_molten,aes(x=name,y=value,fill=variable))+ geom_bar(stat='identity', width=.6)+ scale_fill_discrete(labels=c("Germline","Somatic 5-15% VAF","Somatic 15-30% VAF"))+ geom_text(aes(label=value),size=3,fontface='bold',position=position_stack(vjust=.5))+ xlab("Panel")+ylab("Counts")+ theme_bw()+ theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),panel.background=element_blank(),axis.line=element_line(colour="black"),panel.border=element_blank(),legend.title=element_blank()) gg <- gg + scale_y_continuous(expand = expand_scale(mult=c(0, 0.0))) # get the sacle values of the current y-axis gb <- ggplot_build(gg) y.range <- gb$layout$panel_params[[1]]$y.range y2.range <- range(df_molten$Percent_targeted)# extendrange(, f=0.01) scale_factor <- (diff(y.range)/max(y2.range)) trans <- ~ ((. -y.range[1])/scale_factor) df_molten$Percent_targeted_scaled <- rescale(df_molten$Percent_targeted, y.range, c(0, y2.range[2])) df_molten$x <- which(levels(df_molten$name)%in%df_molten$name)#-.3 # gg <- gg + geom_segment(aes(x=x, xend=x, yend=Percent_targeted_scaled), y=0, size=2, data=df_molten) # gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=Percent_targeted_scaled), fill='white', data=df_molten) gg <- gg + geom_hline(yintercept = y.range[2], linetype='longdash') gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=Percent_targeted_scaled), fill='white', data=df_molten, vjust=0) gg <- gg + scale_y_continuous(expand=expand_scale(mult=c(.05, .05)), sec.axis = sec_axis(trans, name = paste0("Percent genes targeted on ", levels(df_molten$name)[1]), labels = scales::percent(seq(0, 1, length.out = 5), scale=100))) gg 上的最大值相对应。

所以你的意思是这样的吗?

AcroMetrix

enter image description here

原始答案:

根据您提供的数据,在我看来,每个面板上的100%都不相同。

但是,您可以这样执行请求:

library(ggplot2)
library(reshape2)
library(scales)
df<-data.frame(row.names=c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"),Germline=c(34,33,14,22,12),Somatic_5_15=c(341,331,281,249,147),Somatic_15_30=c(180,176,129,124,108))

df$name<-row.names(df)

df_molten<-melt(df)

df_molten$name<-factor(df_molten$name,levels=c('AcroMetrix','PV1_PV2','CHIPv2','TSACP','TSTP'))
df_molten$Percent_targeted <- unlist(lapply(1:length(levels(df_molten$variable)), function(i){c(100,100,77,73,49)}))

gg <- ggplot(df_molten,aes(x=name,y=value,fill=variable))+
  geom_bar(stat='identity', width=.6)+
  scale_fill_discrete(labels=c("Germline","Somatic 5-15% VAF","Somatic 15-30% VAF"))+
  geom_text(aes(label=value),size=3,fontface='bold',position=position_stack(vjust=.5))+
  xlab("Panel")+ylab("Counts")+
  theme_bw()+
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),panel.background=element_blank(),axis.line=element_line(colour="black"),panel.border=element_blank(),legend.title=element_blank())
gg <- gg + scale_y_continuous(expand = expand_scale(mult=c(0, 0.0)))


# get the sacle values of the current y-axis
gb <- ggplot_build(gg)
y.range <- gb$layout$panel_params[[1]]$y.range
y2.range <- range(df_molten$Percent_targeted)# extendrange(, f=0.01)
scale_factor <- (diff(y.range)/max(y2.range))
trans <- ~ ((. -y.range[1])/scale_factor)

df_molten$Percent_targeted_scaled <- rescale(df_molten$Percent_targeted, y.range, c(0, y2.range[2]))
df_molten$x <- which(levels(df_molten$name)%in%df_molten$name)#-.3

# gg <- gg + geom_segment(aes(x=x, xend=x, yend=Percent_targeted_scaled), y=0, size=2, data=df_molten)
# gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=Percent_targeted_scaled), fill='white', data=df_molten)
gg <- gg + geom_hline(yintercept = y.range[2], linetype='longdash')
gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=y.range[2]+5), fill='white', data=df_molten, vjust=0)

gg <- gg + scale_y_continuous(expand=expand_scale(mult=c(.05, .05)), sec.axis = sec_axis(trans, name = paste0("Percent genes targeted on ", levels(df_molten$name)[1]), labels = scales::percent(seq(0, 1, length.out = 5), scale=100)))
gg          

enter image description here