Y 轴上具有高范围的散点图
Scatter plot having high range on Y-axis
如何为我的 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))
如果 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"))
如何为我的 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))
如果 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"))