gpt4 book ai didi

r - 在 ggplot2 中;在这种情况下,如何将 x 轴移动到顶部,然后使用 coord_flip () 旋转轴?

转载 作者:行者123 更新时间:2023-12-04 03:56:14 60 4
gpt4 key购买 nike

我试图将 x 轴移动到顶部:scale_x_discrete (position = "top") 但它指示错误。有人帮帮我吗?

require(ggplot2)
a <- c("ENE","FEB","MAR","ABR","MAY","JUN","JUL","AGO","SET","OCT","NOV","DIC")
b <- c(0,0,0,0,18,133,316,55,242,36,13,0)
df <- data.frame("mes"=a,"tot"=b)

p <- ggplot(df, aes(x=mes, y=tot))+
geom_col(colour="black", fill="gray")+
labs(x="meses", y="ocurrencias")+
theme_bw()+
theme(
panel.grid.minor=element_blank(),
plot.title = element_text(size=16,face="bold",hjust=0.5),
plot.subtitle = element_text(size=15,hjust=0.5),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.text=element_text(size=12,face="bold"),
axis.title=element_text(size=14,face="bold")
)+
geom_text(aes(label=mes),
vjust=0.5, hjust=-0.2,
color='black', size=5, fontface = "bold")
p <- p + coord_flip()
plot(p)

最佳答案

这可以像这样实现。

  1. 交换 xy 而不是使用 coord_flip
  2. 使用scale_x_continuous(position = "top")

或者坚持使用 coord_flip 并使用 scale_y_continuous(position = "right")(翻转后 y 变为 x,右变为顶部)。 (;

library(ggplot2)
a<-c("ENE","FEB","MAR","ABR","MAY","JUN","JUL","AGO","SET","OCT","NOV","DIC")
b<-c(0,0,0,0,18,133,316,55,242,36,13,0)
df<-data.frame("mes"=a,"tot"=b)

p<-ggplot(df,aes(y=mes, x=tot))+
geom_col(
colour="black",fill="gray")+
labs(x="meses", y="ocurrencias")+theme_bw()+
theme(
panel.grid.minor=element_blank(),
plot.title = element_text(size=16,face="bold",hjust=0.5),
plot.subtitle = element_text(size=15,hjust=0.5),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.text=element_text(size=12,face="bold"),
axis.title=element_text(size=14,face="bold")
)+
geom_text(
aes(label=mes),vjust=0.5,hjust=-0.2,
color='black', size=5,fontface = "bold") +
scale_x_continuous(position = "top")
p

关于r - 在 ggplot2 中;在这种情况下,如何将 x 轴移动到顶部,然后使用 coord_flip () 旋转轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63863398/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com