如何在刻面图上的刻度线上结束y轴?

时间:2018-04-12 15:34:50

标签: r ggplot2 yaxis

我使用facet_wrap从数据框中制作了几个图。我的问题是格式化y轴。我在pretty_breaks中使用scale_y_continuous来平均y轴上的刻度线。我现在想要在一个刻度线上结束y轴,并且它看起来并不直接,因为我的图是刻面的。

那么,我有什么: enter image description here enter image description here

我想要的: enter image description here enter image description here

希望这是有道理的!谢谢你的帮助

以下是数据:

   ID Age Sex Genotype  Organ Weight Ratio
35  1 P22   F        b   Body    9.2  1.00
36  1 P22   F        b  Heart   78.4  8.52
37  1 P22   F        b   Lung  156.2 16.98
38  1 P22   F        b  Liver  492.1 53.49
39  1 P22   F        b Spleen   44.9  4.88
40  1 P22   F        b  Brain  313.2 34.04
41  2 P22   F        a   Body    9.3  1.00
42  2 P22   F        a  Heart   69.3  7.45
43  2 P22   F        a   Lung  225.6 24.26
44  2 P22   F        a  Liver  512.3 55.09
45  2 P22   F        a Spleen   69.1  7.43
46  2 P22   F        a  Brain  373.2 40.13

以下是代码:

# Load file and find names
df <- read.csv('yaxis_ticks_data.csv')

# Subsetting to remove Body ratio
organ.ratios <- subset(df, df$Organ != 'Body')

## P22 Ratio plots

# Grouping for geom_point
pointGroup <- group_by(organ.ratios, Organ, Genotype, ID, Ratio)
pointGroup.Summary <- summarise(pointGroup,
                            n = n())
pointGroup.Summary

# Grouping for geom_bar
barGroup <- group_by(pointGroup.Summary, Organ, Genotype)
barGroup
barGroup.Summary <- summarise(barGroup,
                          mean_Ratio = mean(Ratio),
                          n = n())
barGroup.Summary

# Plot
P22.Organ.Body.plot <- ggplot() +
geom_bar(data = barGroup.Summary, 
       aes(x = barGroup.Summary$Genotype,
           y = barGroup.Summary$mean_Ratio,
           colour = Genotype,
           fill = Genotype),  
       position = position_dodge(width = 0.9), 
       stat = 'identity',
       show.legend = T) +
geom_point(data = pointGroup.Summary, 
         aes(x = pointGroup.Summary$Genotype, 
             y = pointGroup.Summary$Ratio, 
             colour = Genotype), 
         position = position_jitterdodge(jitter.width = 0.2, dodge.width = 0.9), 
         stat = 'identity', 
         show.legend = FALSE) +
facet_wrap(~Organ, scales = 'free', nrow = 1) +
xlab(expression(bold('Genotype'))) +
ylab(expression(bold('Organ/Body Ratio'))) +
theme(axis.line = element_line(colour = 'black'),
    strip.background = element_blank(),
    strip.text = element_text(face = 'bold'),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    panel.border = element_blank(),
    panel.background = element_blank(),
    legend.text.align = 0,
    legend.title = element_text(face = 'bold'),
    text = element_text(family = 'Arial', size = 14)) +
 scale_color_manual('Genotype', 
                 labels = c('a', 'b'), 
                 values = c('black', 'black')) +
 scale_fill_manual('Genotype', 
                labels = c('a', 'b'), 
                values = c('white', 'deepskyblue3')) +
 scale_y_continuous(breaks = pretty_breaks(), expand = expand_scale(mult = c(0, 0.1)))


#Get plot
P22.Organ.Body.plot

1 个答案:

答案 0 :(得分:0)

这是一个简单的实现,用于计算每个方面所需的y轴刻度值:

library(scales)
library(dplyr)

df.2 <- df %>%
  filter(Organ != "Body") %>%
  select(Genotype, Organ, Ratio) %>%
  group_by(Organ) %>%
  mutate(max.y.tick = max(pretty_breaks()(c(0, Ratio)))) %>%
  ungroup()

> df.2
# A tibble: 10 x 4
   Genotype  Organ Ratio max.y.tick
     <fctr> <fctr> <dbl>      <dbl>
 1        b  Heart  8.52         10
 2        b   Lung 16.98         25
 3        b  Liver 53.49         60
 4        b Spleen  4.88          8
 5        b  Brain 34.04         50
 6        a  Heart  7.45         10
 7        a   Lung 24.26         25
 8        a  Liver 55.09         60
 9        a Spleen  7.43          8
10        a  Brain 40.13         50

然后可以通过geom_blank()将此值传递给ggplot对象的每个方面:

ggplot(df.2, 
       aes(x = Genotype, y = Ratio, fill = Genotype)) +
  geom_col() +
  geom_point(show.legend = FALSE) +
  scale_y_continuous(breaks = pretty_breaks(),
                     expand = c(0, 0)) +
  geom_blank(aes(y = max.y.tick)) +
  facet_wrap(~Organ, scales = "free_y", nrow = 1) 

(我跳过所有主题/标签组件,因为它们与问题的症结无关。)

plot