Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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,我正在用ggplot2从这个数据框中制作一个条形图,以比较Uniprot和macki列中的数据 Famille Uniprot Macki SN_02 26 7 SN_03 21 22 SN_04 16 7 SN_05 4 0 SN_09 10 0 SN_10 5 0 SN_17 6

我正在用ggplot2从这个数据框中制作一个条形图,以比较Uniprot和macki列中的数据

 Famille Uniprot    Macki
 SN_02     26           7
 SN_03     21          22
 SN_04     16           7
 SN_05      4           0
 SN_09     10           0
 SN_10      5           0
 SN_17      6           0
 SN_19     13           4
 SN_20      3           1
 SN_31      2           0
 SN_32      5           3
 SN_33      3           0
 SN_34      1           0
 SN_37      3           0
这是我的R代码:

bar <- c(26, 21, 16,4,10,5,6,13,3,2,5,3,1,3,7,22,7,0,0,0,0,4,1,0,3,0,0,0)
lab <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17","SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37")
pct <- round(bar/sum(bar)*100)
lab <- paste(lab,"%", sep = "")
lab <- paste(lab, pct)
lab0 <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37")
nlab <- length(lab)
type <- rep("Uniprot:MACKI", each=nlab/2)
library(ggplot2)
theme_set(theme_bw())
dd <- data.frame(lab0, type, bar, pct)
dd$lab0 <- reorder(dd$lab0,-dd$bar)
ggplot(dd,aes(x=lab0,y=bar,fill=lab0))+geom_bar(aes(alpha=factor(bar)), stat = "identity",
position=position_dodge(width=1))+scale_alpha_discrete(range=c(0.5,1.0))+
geom_text(aes(label=paste0(pct,"%"),group=interaction(lab0,type)),hjust=-0.5, position=position_dodge(width=1))
+ coord_flip()+expand_limits(y=20)+labs(x="",y="total")

bar通过
+
将对象添加到ggplot2,必须在同一行中。在最后一行之前看一看-你必须把
+
放在那里。修改后代码生效

bar <- c(26, 21, 16,4,10,5,6,13,3,2,5,3,1,3,7,22,7,0,0,0,0,4,1,0,3,0,0,0)
lab <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17","SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37")
pct <- round(bar/sum(bar)*100)
lab <- paste(lab,"%", sep = "")
lab <- paste(lab, pct)
lab0 <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37")
nlab <- length(lab)
type <- rep("Uniprot:MACKI", each=nlab/2)
library(ggplot2)
theme_set(theme_bw())
dd <- data.frame(lab0, type, bar, pct)
dd$lab0 <- reorder(dd$lab0,-dd$bar)
ggplot(dd,aes(x=lab0,y=bar,fill=lab0))+
  geom_bar( aes(alpha=factor(bar)) , stat = "identity", position = position_dodge(width=1) ) +
  scale_alpha_discrete( range=c(0.5,1.0) )+
  geom_text( 
    aes( label=paste0(pct,"%") , group=interaction(lab0,type) ), 
    hjust = -0.5, 
    position = position_dodge(width=1)) +
  coord_flip()+
  expand_limits(y = 20) +
  labs(x = "",y = "total")