R:一张图中Y轴多条线的不同值
R: Different values of multiple lines on Y-axis in one graph
前段时间我做了一个图表,其中包含多条具有不同值集的线。结果是这张图像在 y 轴上的值作为部分 od 值:http://imgur.com/a/aLRUC
一段时间后,我使用了完全相同的代码,但只是稍微更改了 table 中的一些值,输出是具有连续 y 轴的图像:http://imgur.com/v6DLB09
在这两种情况下,我都使用了完全相同的代码,但我得到了两次不同的输出。我想获得第一个输出,其中 y 轴按部分显示,以便更好地显示多年来的值偏差。谁能建议我该怎么做?
我使用的数据是 *.csv table,它有 6 列,其值代表我研究区域的土地利用百分比
Sensor Acquisition_time Land Sea Lagoon River
Landsat_4 1992 72.79 19.05 7.56 0.60
Landsat_5 1984 72.96 19.17 7.02 0.85
Landsat_5 1988 72.82 19.41 7.09 0.68
Landsat_5 1996 73.46 19.27 6.71 0.56
Landsat_5 2000 72.72 19.23 7.43 0.62
Landsat_5 2004 72.48 19.05 7.78 0.69
Landsat_5 2008 72.67 19.14 7.49 0.70
Landsat_8 2013 72.66 19.10 7.49 0.75
Landsat_8 2016 72.81 19.03 7.38 0.78
我使用的代码是:
table <- read.csv("results.csv", header=TRUE)
mtbl <- melt(table, id.vars="Acquisition_time", measure.vars = c("Land", "Sea", "Lagoon", "River"))
#draw a graph
ggplot(data=mtbl, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years", breaks = mtbl$Acquisition_time)
我应该添加什么来获得我之前自动获得的 y 轴上的离散值?
跟你的值有关,第一个例子是一个因素,第二个例子是一个连续值。这是一个可重现的例子:
Acquisition_time <- c(1992,1984,1988,1996,2000,2004,2008,2013,2016)
Land <- c(72.79,72.96,72.82,73.46,72.72,72.48,72.67,72.66,72.81)
Sea <- c(19.05,19.17,19.41,19.27,19.23,19.05,19.14,19.10,19.03)
Lagoon <- c(7.56,7.02,7.09,6.71,7.43,7.78,7.49,7.49,7.38)
River <- c(0.60,0.85,0.68,0.56,0.62,0.69,0.70,0.75,0.78)
table <- data.frame(Acquisition_time, Land, Sea, Lagoon, River)
library(tidyr)
library(dplyr)
library(ggplot2)
mtbl <- table %>% gather(variable, value, -Acquisition_time)
mtblfac <- mtbl %>% mutate(value = factor(value))
# with value as numeric
ggplot(data=mtbl, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years")
# with value as factor
ggplot(data=mtblfac, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years")
但我建议您像下面的示例一样使用构面和连续值,因为您保留了值之间的关系,而不是使它们分类。但是使用 scales="free"
选项,您可以区分差异,就像您的第一个示例一样。
# with value as numeric and facets
ggplot(data=mtbl, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years") +
facet_grid(variable~., scales="free")
前段时间我做了一个图表,其中包含多条具有不同值集的线。结果是这张图像在 y 轴上的值作为部分 od 值:http://imgur.com/a/aLRUC 一段时间后,我使用了完全相同的代码,但只是稍微更改了 table 中的一些值,输出是具有连续 y 轴的图像:http://imgur.com/v6DLB09
在这两种情况下,我都使用了完全相同的代码,但我得到了两次不同的输出。我想获得第一个输出,其中 y 轴按部分显示,以便更好地显示多年来的值偏差。谁能建议我该怎么做? 我使用的数据是 *.csv table,它有 6 列,其值代表我研究区域的土地利用百分比
Sensor Acquisition_time Land Sea Lagoon River
Landsat_4 1992 72.79 19.05 7.56 0.60
Landsat_5 1984 72.96 19.17 7.02 0.85
Landsat_5 1988 72.82 19.41 7.09 0.68
Landsat_5 1996 73.46 19.27 6.71 0.56
Landsat_5 2000 72.72 19.23 7.43 0.62
Landsat_5 2004 72.48 19.05 7.78 0.69
Landsat_5 2008 72.67 19.14 7.49 0.70
Landsat_8 2013 72.66 19.10 7.49 0.75
Landsat_8 2016 72.81 19.03 7.38 0.78
我使用的代码是:
table <- read.csv("results.csv", header=TRUE)
mtbl <- melt(table, id.vars="Acquisition_time", measure.vars = c("Land", "Sea", "Lagoon", "River"))
#draw a graph
ggplot(data=mtbl, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years", breaks = mtbl$Acquisition_time)
我应该添加什么来获得我之前自动获得的 y 轴上的离散值?
跟你的值有关,第一个例子是一个因素,第二个例子是一个连续值。这是一个可重现的例子:
Acquisition_time <- c(1992,1984,1988,1996,2000,2004,2008,2013,2016)
Land <- c(72.79,72.96,72.82,73.46,72.72,72.48,72.67,72.66,72.81)
Sea <- c(19.05,19.17,19.41,19.27,19.23,19.05,19.14,19.10,19.03)
Lagoon <- c(7.56,7.02,7.09,6.71,7.43,7.78,7.49,7.49,7.38)
River <- c(0.60,0.85,0.68,0.56,0.62,0.69,0.70,0.75,0.78)
table <- data.frame(Acquisition_time, Land, Sea, Lagoon, River)
library(tidyr)
library(dplyr)
library(ggplot2)
mtbl <- table %>% gather(variable, value, -Acquisition_time)
mtblfac <- mtbl %>% mutate(value = factor(value))
# with value as numeric
ggplot(data=mtbl, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years")
# with value as factor
ggplot(data=mtblfac, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years")
但我建议您像下面的示例一样使用构面和连续值,因为您保留了值之间的关系,而不是使它们分类。但是使用 scales="free"
选项,您可以区分差异,就像您的第一个示例一样。
# with value as numeric and facets
ggplot(data=mtbl, aes(x= Acquisition_time, y=value, group=variable, colour=variable)) +
geom_line() +
geom_point( size=4, shape=21, fill="white") +
scale_x_continuous(name="Years") +
facet_grid(variable~., scales="free")