【问题标题】:Scatter plot having high range on Y-axisY 轴上具有高范围的散点图
【发布时间】:2020-11-10 16:01:03
【问题描述】:

如何为我的 y 轴范围很大的数据制作散点图? 我想在 X 轴上拥有 year,在 Y 轴上拥有 Size,同时拥有多年不同的颜色。 我的问题的重点是如何在 y 轴上显示大小之间的差异。所以,ggplot(df, aes(x = Year, y = Size)) + geom_point(aes(color = factor(Year))) 不是答案。

structure(list(Year = c(2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 
2008L, 2009L, 2009L, 2010L, 2012L, 2012L, 2013L, 2014L, 2014L, 
2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2020L, 2020L, 2020L, 2020L, 2020L), Size = structure(c(19L, 
1L, 27L, 27L, 19L, 14L, 23L, 9L, 28L, 14L, 20L, 20L, 10L, 3L, 
23L, 31L, 17L, 18L, 28L, 25L, 5L, 25L, 25L, 13L, 28L, 5L, 15L, 
16L, 7L, 4L, 21L, 8L, 6L, 6L, 25L, 2L, 30L, 33L, 12L, 29L, 11L, 
22L, 25L, 26L, 33L, 25L, 32L, 24L, 2L), .Label = c("10", "100", 
"102", "105", "108", "126", "139", "142", "17", "20", "20 389", 
"200", "25", "27", "30", "300", "35", "40", "43", "44", "46", 
"5 482 ", "50", "500", "507", "52 735 ", "70", "75", "80", "81", 
"83", "95", "96"), class = "factor")), class = "data.frame", row.names = c(NA, 
-49L))

【问题讨论】:

    标签: r plot graph scatter-plot


    【解决方案1】:

    如果 y 轴太大,值范围很大,一个不错的选择是对数刻度。我将使用以 10 为底的对数作为 y 轴刻度。

    首先,您的数据以Size 为因子,如果您以数字形式读取它,则不再需要此步骤。

    df1$Size <- as.numeric(gsub(" ", "", as.character(df1$Size)))
    

    现在是剧情。

    library(ggplot2)
    
    Breaks <- min(df1$Year):max(df1$Year)
    
    ggplot(df1, aes(Year, Size, color = factor(Year))) +
      geom_point() + 
      scale_x_continuous(breaks = Breaks, label = Breaks) +
      scale_y_log10() +
      labs(y = bquote(log[10](Size))) +
      guides(color = guide_legend(title = "Year"))
    

    【讨论】:

    • 谢谢@Rui Barradas。如何必须在 y 轴上设置上限值?我将上限值设置为 100,000,它完全重塑了整个图形。
    • @EsterSilva 您要求设置更大的数量级。这很多,整个图表看起来会有所不同。你试过scale_y_log10(limits = c(1, 100000))吗?
    • @EsterSilva 请参阅this post 以获得漂亮的对数刻度标签。
    • 谢谢@Rui Barradas。当我将您的评论编辑为 scale_y_log10(limits = c(10, 100000))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多