您好,登錄后才能下訂單哦!
本篇內容主要講解“用R語言畫柱形圖怎么讓屁股朝右”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“用R語言畫柱形圖怎么讓屁股朝右”吧!
因為A圖的數據較多,我們來模仿B圖,過程其實是一樣的
最左邊的屁股朝右的柱形圖還是第一次畫,查了一下可以借助
scale_y_reverse()
函數實現
set.seed(1234)
x<-seq(5,10,by=0.5)
df<-data.frame(`s__Klebsiella_phage_vB_KpnP_SU552A` = sample(x,10,replace = T),
`s__Escherichia_phage_ECBP5` = sample(x,10,replace = T),
`s__Clostridium_phage_phi8074-B1` = sample(x,10,replace = T),
check.names = F)
head(df)
df%>%
reshape2::melt() -> df1
library(dplyr)
df%>%
reshape2::melt()%>%
group_by(variable)%>%
summarise(mean_value=mean(value),
sd_value=sd(value)) -> df2
ggplot()+
geom_col(data=df2,aes(x=variable,y=mean_value),
fill="#8babd3",
color="black")+
geom_errorbar(data=df2,aes(x=variable,
ymin=mean_value-sd_value,
ymax=mean_value+sd_value),
width=0.2)+
geom_jitter(data=df1,aes(x=variable,y=value),
width = 0.2,color="grey")
ggplot()+
geom_col(data=df2,aes(x=variable,y=mean_value),
fill="#8babd3",
color="black")+
geom_errorbar(data=df2,aes(x=variable,
ymin=mean_value-sd_value+0.001,
ymax=mean_value+sd_value),
width=0.2)+
geom_jitter(data=df1,aes(x=variable,y=value),
width = 0.2,color="grey")+
#scale_y_continuous(expand = c(0,0))+
theme_bw()+
coord_flip()+
scale_y_reverse(expand=c(0,0),
position="right")+
labs(x=NULL,y=NULL)
ggplot()+
geom_col(data=df2,aes(x=variable,y=mean_value),
fill="#ffc080",
color="black")+
geom_errorbar(data=df2,aes(x=variable,
ymin=mean_value-sd_value+0.001,
ymax=mean_value+sd_value),
width=0.2)+
geom_jitter(data=df1,aes(x=variable,y=value),
width = 0.2,color="grey")+
scale_y_continuous(expand = c(0,0),
position = "right")+
theme_bw()+
coord_flip()+
labs(x=NULL,y=NULL)+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank())
df3<-data.frame(x="A",
y=c("s__Klebsiella_phage_vB_KpnP_SU552A",
"s__Escherichia_phage_ECBP5",
"s__Clostridium_phage_phi8074-B1"),
group=c("f__Siphoviridae",
"f__Podoviridae",
"f__Podoviridae"))
ggplot(df3,aes(x=x,y=y))+
geom_tile(aes(fill=group),show.legend = F)+
labs(x=NULL,y=NULL)+
scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0))+
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())+
scale_fill_manual(values = c("#c65911","#ffd965"))
library(patchwork)
p1+p2+p3+ggtitle("Bacteriophages")+
theme(plot.title = element_text(hjust=5))+
plot_layout(widths = c(1.2,1,0.2)) -> p
ggsave(filename = "Rplot10.pdf",
p,
width = 10,height = 3)
最終的結果如下
到此,相信大家對“用R語言畫柱形圖怎么讓屁股朝右”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。