【问题标题】:Creating ggplot geom_point() with position dodge 's-shape'使用位置闪避's-shape'创建ggplot geom_point()
【发布时间】:2021-06-20 14:33:36
【问题描述】:

我正在尝试创建一个如下图所示的情节。我希望将每个类别中的点排列成一个 s 形。可以在ggplot中做到这一点吗?

类似的数据可用here

到目前为止我所拥有的:

somatic.variants <- read.delim("data/Lawrence.S2.txt", stringsAsFactors=T)
cancer_rates <- tapply(somatic.variants$logn_coding_mutations, somatic.variants$tumor_type, median)
cancer_rates <- cancer_rates[order(cancer_rates, decreasing=F)]
somatic.variants$tumor_type <- factor(somatic.variants$tumor_type, levels = names(cancer_rates))

library(ggplot2)
library(GGally)
ggplot(data = somatic.variants,
       mapping = aes(x = tumor_type,
                     y = log10(n_coding_mutations))) +
  geom_point(position = position_dodge2()) +
  scale_x_discrete(position = "top") +
  scale_y_continuous(labels = c(0,10,100,1000,10000), expand = c(0,0)) +
  geom_stripped_cols() +
  theme_bw() +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 315, hjust = 1, size = 12),
        panel.grid = element_blank()) +
  labs(y = "Coding mutations count") +
  stat_summary(fun = median,
               geom="crossbar",
               size = 0.25,
               width = 0.9,
               group = 1,
               show.legend = FALSE,
               color = "#FF0000")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这可以通过

    来实现
    1. 按 x 轴类别对数据进行分组
    2. 按 y 轴值排列

    确保按每个类别的值的升序绘制点。

    somatic.variants <- read.delim("https://gist.githubusercontent.com/wudustan/57deecdaefa035c1ecabf930afde295a/raw/1594d51a1e3b52f674ff746caace3231fd31910a/Lawrence.S2.txt", stringsAsFactors=T)
    cancer_rates <- tapply(somatic.variants$logn_coding_mutations, somatic.variants$tumor_type, median)
    cancer_rates <- cancer_rates[order(cancer_rates, decreasing=F)]
    somatic.variants$tumor_type <- factor(somatic.variants$tumor_type, levels = names(cancer_rates))
    
    library(ggplot2)
    library(GGally)
    library(dplyr)
    
    somatic.variants <- somatic.variants %>% 
      group_by(tumor_type) %>% 
      arrange(n_coding_mutations)
    
    ggplot(data = somatic.variants,
           mapping = aes(x = tumor_type,
                         y = log10(n_coding_mutations))) +
      geom_point(position = position_dodge2(.9), size = .25) +
      scale_x_discrete(position = "top") +
      scale_y_continuous(labels = c(0,10,100,1000,10000), expand = c(0,0)) +
      geom_stripped_cols() +
      theme_bw() +
      theme(axis.title.x = element_blank(),
            axis.text.x = element_text(angle = 315, hjust = 1, size = 12),
            panel.grid = element_blank()) +
      labs(y = "Coding mutations count") +
      stat_summary(fun = median,
                   geom="crossbar",
                   size = 0.25,
                   width = 0.9,
                   group = 1,
                   show.legend = FALSE,
                   color = "#FF0000")
    #> Warning: Removed 29 rows containing non-finite values (stat_summary).
    

    【讨论】:

    • 非常感谢您。正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多