如何为在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))
答案 0 :(得分:2)
如果y轴太大,值范围很大,则对数刻度是个不错的选择。 y轴比例将以10为底的对数。
首先,您的数据具有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"))