【问题标题】:How to reverse axis y predetermined from scale_y_log10? ggplot [repeated]如何反转从 scale_y_log10 预定的轴 y? ggplot [重复]
【发布时间】:2020-07-30 02:52:07
【问题描述】:

Similar here

我正在尝试用已经预先确定的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


    【解决方案1】:

    已提供答案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)
    

    【讨论】:

    • 对不起,我看到了帖子,但我不明白它适用于我的情况。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2023-03-03
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多