【发布时间】:2020-07-30 02:52:07
【问题描述】:
我正在尝试用已经预先确定的scale_y_log10 绘制一个图形,同时反转y axis,但是当我这样做时,没有工作,显示一条消息
“y”的比例已经存在。为“y”添加另一个比例,它将替换现有比例。
dt = data.table(area= c("BA", "FA", "MI"),
dmean = c(30, 50, 200, 76, 467, 87, 98, 10, 240, 176, 89, 400, 340, 10, 40, 54, 89, 340, 205),
sex = c("F", "M"))
leg<- c("Breeding area", "Migration", "Feeding area") #to rename
dt$area<- factor(dt$area, levels = c("BA", "MI", "FA")) #to reorder
ggplot(dt, mapping = aes(y = dmean, x = area, color = sex, fill=sex))+
geom_violin(alpha=.5,scale = "width",trim = FALSE, position=position_dodge(1))+
ggtitle("Dive mean per area and sex")+
scale_y_log10(breaks = c(10, 30, 50, 100, 200, 300, 400, 500)) +
scale_fill_discrete(name="Social class",
labels=c("Female", "Male"))+
xlab("Habitat")+
ylab("Dive depth (m)")+
theme_bw()+
scale_x_discrete(labels= leg)
我尝试了很多东西:
scale_y_reverse(breaks = c(500, 400, 300, 100, 50,30, 10))+ #1
scale_y_reverse()+ #2
scale_y_continuous(trans = scales::reciprocal_trans(), trans= c(600, 500, 400, 300, 100, 50, 10))+ #3
scale_y_continuous(trans = "reverse", breaks = unique(dt$dmean))+ #4
scale_y_continuous(trans = "reverse")+ #5
scale_y_discrete(limits = rev(levels(dt)))+ #6
scale_y_discrete(limits = rev(levels(dt$dmean)))+ #7
scale_y_discrete(limits = rev(levels(as.factor(dt$dmean))))+ #8
scale_y_discrete(drop = FALSE)+ #9
gplot(dt, mapping = aes(y = reorder(dmean, desc(dmean)), x = area, color = sex, fill=sex))+ #10
dt<-dt %>% mutate(position = factor(dmean),
position = factor(dmean, levels = rev(levels(dmean)))%>% #11
ggplot(dt, mapping = aes(y = factor(dmean, levels=c(1:600)), x = area, color = sex, fill=sex))+ #12
ggplot(dt, mapping = aes(y = forcats::fct_rev(factor(dmean)), x = area, color = sex, fill=sex))+ #13
dt$dmean <- factor(dt$dmean, levels = rev(levels(dt$dmean)))+ #14
有人知道如何解决这个问题吗? 谢谢!
【问题讨论】:
标签: r ggplot2 reverse axis yaxis