ggplot2 - 更改 legend/variable 名称并删除 x 轴上的空白 space
ggplot2 - Change legend/variable names and remove blank space in x axis
我有以下数据:
DAYS;GWFDisi;GSWIM;GGAP3;GHYPE;GVIC;HWFDisi;HSWIM;HGAP3;HHYPE;HVIC;IWFDisi
1;-308.78;-183.19;-232.48;-233.22;-150.38;-596.49;-311.58;-571.41;-387.63;-315.43;-451.26
2;-348.52;-192.39;-314.68;-231.67;-147.88;-563.14;-335.39;-558.46;-423.13;-355.14;-501.58
3;-416.24;-211.68;-436.83;-232.73;-164.87;-537.54;-327.19;-465.70;-455.62;-403.40;-458.43
4;-459.95;-217.75;-486.37;-228.07;-202.23;-560.68;-359.07;-497.20;-481.41;-430.87;-475.76
5;-437.58;-219.63;-494.34;-223.27;-249.18;-613.41;-371.47;-457.38;-499.42;-433.01;-446.02
6;-470.20;-228.91;-503.95;-217.41;-292.13;-618.50;-381.87;-505.86;-505.63;-430.23;-440.30
7;-500.54;-245.91;-527.91;-226.86;-319.97;-599.95;-381.06;-416.05;-474.56;-431.76;-526.32
我正在使用以下代码绘制此数据:
rm(list = ls())
setwd("C:/Users/stevens/Desktop/daily_days")
almourol_diff <- read.table("data.csv", header = TRUE, sep = ";", fill = TRUE)
library(ggplot2)
library(reshape2)
data <- melt(data, id.vars="DAYS")
png("data.png", width = 600, height = 400)
p <- ggplot(data, aes(DAYS,value, col=variable))
p + geom_line(size = 0.1) +
geom_hline(aes(yintercept = 0), linetype="dotted") +
ylab("Diff in runoff [m3/s]") +
theme_bw() +
theme(legend.key = element_blank())
graphics.off()
这给了我以下输出:
我的问题如下:
- 如何更改当前设置为 "variable" 的图例名称?
- 我怎样才能去掉 x 轴末端的 with space?
要去掉x轴上下边界的白色space,应该加上:
+ scale_x_continuous(expand = c(0, 0))
第一个参数是乘法因子,第二个是加法因子,两者都表示应该向 x 轴添加多少白色 space。这也适用于 y 尺度。
在 http://docs.ggplot2.org/current/scale_continuous.html 查看更多信息。
我有以下数据:
DAYS;GWFDisi;GSWIM;GGAP3;GHYPE;GVIC;HWFDisi;HSWIM;HGAP3;HHYPE;HVIC;IWFDisi
1;-308.78;-183.19;-232.48;-233.22;-150.38;-596.49;-311.58;-571.41;-387.63;-315.43;-451.26
2;-348.52;-192.39;-314.68;-231.67;-147.88;-563.14;-335.39;-558.46;-423.13;-355.14;-501.58
3;-416.24;-211.68;-436.83;-232.73;-164.87;-537.54;-327.19;-465.70;-455.62;-403.40;-458.43
4;-459.95;-217.75;-486.37;-228.07;-202.23;-560.68;-359.07;-497.20;-481.41;-430.87;-475.76
5;-437.58;-219.63;-494.34;-223.27;-249.18;-613.41;-371.47;-457.38;-499.42;-433.01;-446.02
6;-470.20;-228.91;-503.95;-217.41;-292.13;-618.50;-381.87;-505.86;-505.63;-430.23;-440.30
7;-500.54;-245.91;-527.91;-226.86;-319.97;-599.95;-381.06;-416.05;-474.56;-431.76;-526.32
我正在使用以下代码绘制此数据:
rm(list = ls())
setwd("C:/Users/stevens/Desktop/daily_days")
almourol_diff <- read.table("data.csv", header = TRUE, sep = ";", fill = TRUE)
library(ggplot2)
library(reshape2)
data <- melt(data, id.vars="DAYS")
png("data.png", width = 600, height = 400)
p <- ggplot(data, aes(DAYS,value, col=variable))
p + geom_line(size = 0.1) +
geom_hline(aes(yintercept = 0), linetype="dotted") +
ylab("Diff in runoff [m3/s]") +
theme_bw() +
theme(legend.key = element_blank())
graphics.off()
这给了我以下输出:
我的问题如下: - 如何更改当前设置为 "variable" 的图例名称? - 我怎样才能去掉 x 轴末端的 with space?
要去掉x轴上下边界的白色space,应该加上:
+ scale_x_continuous(expand = c(0, 0))
第一个参数是乘法因子,第二个是加法因子,两者都表示应该向 x 轴添加多少白色 space。这也适用于 y 尺度。
在 http://docs.ggplot2.org/current/scale_continuous.html 查看更多信息。