如何反转从 scale_y_log10 预先确定的 y 轴? ggplot [重复]

How to reverse axis y predetermined from scale_y_log10? ggplot [repeated]

Similar here

我正在尝试用已经预先确定的 scale_y_log10 绘制一个图形,同时反转 y axis,但是当我这样做时,没有工作,显示一条消息

Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.

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

有人知道如何解决这个问题吗? 谢谢!

已提供答案here。 将此应用于您的问题,我能够通过在 scale_y_continous:

中应用转换函数来反转对数轴
library("scales")
reverselog_trans <- function(base = exp(1)) {
  trans <- function(x) -log(x, base)
  inv <- function(x) base^(-x)
  trans_new(paste0("reverselog-", format(base)), trans, inv, 
            log_breaks(base = base), 
            domain = c(1e-100, Inf))
}

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_continuous(trans = reverselog_trans(10), 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)