ggpubr ggbarplot用奇异的X值绘制

ggpubr ggbarplot用奇异的X值绘制,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,我有以下使用CSV的代码 library(ggpubr) library(ggsci) df = read.csv2("file.csv", row.names=1) # Copy df df2 = df # Convert the cyl variable to a factor df2$perc <- as.factor(df2$perc) # Add the name colums df2$name <- rownames(df) ggbarplot(df2, x = "nam

我有以下使用CSV的代码

library(ggpubr)
library(ggsci)
df = read.csv2("file.csv", row.names=1)
# Copy df
df2 = df
# Convert the cyl variable to a factor
df2$perc <- as.factor(df2$perc)
# Add the name colums
df2$name <- rownames(df)
ggbarplot(df2, x = "name", y = "perc",
          fill = "role",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "npg",            # jco journal color palett. see ?ggpar
          sort.val = "asc",          # Sort the value in dscending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 0,           # Rotate vertically x axis texts
          rotate = TRUE,
          label = TRUE, label.pos = "out",
          #label = TRUE, lab.pos = "in", lab.col = "white",
          width = 0.5
)
这一情节是由谁造成的

有两件事我不明白:

1) 为什么每个条的x轴刻度与绘制的实际值相对应?我的意思是为什么x轴不是从0到8,在我看来应该是这样。我希望我的解释正确


2) 标签值似乎与y厚度不对齐。我是否缺少一个选项?

老实说,我可能不会在这里使用
ggpubr
。保持ggplot语法通常更安全。也可以说是更少的代码。。。 (此外,如用户teunbrand所评论的,在这种情况下不要使用因子)

单杠有两个不错的选择:

库(tidyverse)
图书馆(GGSTANTE)
图书馆(ggsci)
选项1-使用坐标翻转

ggplot(df2,aes(fct_重新排序(基因,perc),perc,fill=role))+
geom_col()+
几何图形文本(aes(标签=perc),hjust=0)+
比例尺(填充)(npg)+
坐标翻转(ylim=c(0100))+
主题(经典)+
主题(legend.position='top')+
实验室(x=基因,y=百分比)

选项2-使用ggstance软件包 我更喜欢选项2,因为使用GGSTANTE允许与其他绘图进行更灵活的组合

ggplot(df2,aes(perc,fct_-reorder(genes,perc),fill=role))+
geom_colh()+
几何图形文本(aes(标签=perc),hjust=0)+
比例尺(填充)(npg)+
坐标笛卡尔(xlim=c(0100))+
主题(经典)+
主题(legend.position='top')+
实验室(x=基因,y=百分比)

由(v0.3.0)于2020年3月27日创建

数据

df2 <- read_delim("genes;perc;role
GATA-3;7,9;confirmed in this cancer
CCDC74A;6,8;prognostic in this cancer
LINC00621;6,1;none
POLRMTP1;4,1;none
IGF2BP3;3,2;confirmed in this cancer", ";") %>% rownames_to_column("name")
df2%rownames\u to\u列(“名称”)

对于(1):您的x变量可能是一个因素吗?连续x标度最适合数字数据真的,df2$perc感谢这两个选项,这些看起来很优雅。fct_重新排序错误(基因,perc):找不到函数“fct_重新排序”。我这里有一个很好的问题。错误:loadNamespace(i,c(lib.loc,.libPaths()),versionCheck=vI[[i]])中的“tidyverse”的包或命名空间加载失败:命名空间“rlang”0.4.0已加载,但>=0.4.5已加载required@BenoitB. 也许是时候更新你的软件包了?fct_重新订购已经有相当一段时间了。。。如果不能使用此方法,请使用经典的
因子(x,levels=…)
方法
df2 <- read_delim("genes;perc;role
GATA-3;7,9;confirmed in this cancer
CCDC74A;6,8;prognostic in this cancer
LINC00621;6,1;none
POLRMTP1;4,1;none
IGF2BP3;3,2;confirmed in this cancer", ";") %>% rownames_to_column("name")