R中ggplot2的不同点形状
Different point shapes with ggplot2 in R
我想将我的点显示为形状 1(中心点)和形状 2(所有其他点),然后添加这两个形状的图例。
我的代码如下
dd <- read.table(text="
dates NB
15.05.2018 41
8.06.2018 81
20.06.2018 51
02.07.2018 0
14.07.2018 -1
7.08.2018 49
19.08.2018 112
12.09.2018 32
17.12.2018 -4", header=T, stringsAsFactors=FALSE)
dd$dates <- as.Date(dd$dates, "%d.%m.%Y")
library(ggplot2)
center <- subset(dd, dates=="2018-07-02")
ggplot(dd, aes(dates, NB, xend = center$dates, yend = center$NB)) +
geom_segment(color="black") +
geom_point(shape=1, fill="blue", color="black", size=2) +
ylim(-100,150) +
ylab("Normal Baseline [m]") +
xlab("") +
theme_linedraw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
) +
scale_x_date(breaks = dd$dates)
这是我期望达到的结果:
我该怎么办?谢谢。
我会通过在 data.frame 中添加一列来考虑这一点,这样您就可以在 ggplot 调用中将其用作美学。像这样:
library(ggplot2)
dd <- read.table(text="
dates NB
15.05.2018 41
8.06.2018 81
20.06.2018 51
02.07.2018 0
14.07.2018 -1
7.08.2018 49
19.08.2018 112
12.09.2018 32
17.12.2018 -4", header=T, stringsAsFactors=FALSE)
dd$dates <- as.Date(dd$dates, "%d.%m.%Y")
#Add point as a column to dd so you can add it as an aesthetic
dd$point <- ifelse(dd$dates == "2018-07-02", "Center Point", "Other Points")
ggplot(dd, aes(dates, NB, colour = point, shape = point)) +
geom_segment(aes(xend = dd[point == "Center Point","dates"],
yend = dd[point == "Center Point","NB"]),
colour = "black") +
geom_point() +
ylim(-100,150) +
ylab("Normal Baseline [m]") +
xlab("") +
scale_colour_manual("", values = c("Center Point" = "red", "Other Points" = "black")) +
scale_shape_manual("", values = c("Center Point" = 1, "Other Points" = 2)) +
theme_linedraw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(.8, .8)) +
scale_x_date(breaks = dd$dates)
由 reprex package (v0.3.0)
于 2020-05-04 创建
我想将我的点显示为形状 1(中心点)和形状 2(所有其他点),然后添加这两个形状的图例。
我的代码如下
dd <- read.table(text="
dates NB
15.05.2018 41
8.06.2018 81
20.06.2018 51
02.07.2018 0
14.07.2018 -1
7.08.2018 49
19.08.2018 112
12.09.2018 32
17.12.2018 -4", header=T, stringsAsFactors=FALSE)
dd$dates <- as.Date(dd$dates, "%d.%m.%Y")
library(ggplot2)
center <- subset(dd, dates=="2018-07-02")
ggplot(dd, aes(dates, NB, xend = center$dates, yend = center$NB)) +
geom_segment(color="black") +
geom_point(shape=1, fill="blue", color="black", size=2) +
ylim(-100,150) +
ylab("Normal Baseline [m]") +
xlab("") +
theme_linedraw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
) +
scale_x_date(breaks = dd$dates)
这是我期望达到的结果:
我该怎么办?谢谢。
我会通过在 data.frame 中添加一列来考虑这一点,这样您就可以在 ggplot 调用中将其用作美学。像这样:
library(ggplot2)
dd <- read.table(text="
dates NB
15.05.2018 41
8.06.2018 81
20.06.2018 51
02.07.2018 0
14.07.2018 -1
7.08.2018 49
19.08.2018 112
12.09.2018 32
17.12.2018 -4", header=T, stringsAsFactors=FALSE)
dd$dates <- as.Date(dd$dates, "%d.%m.%Y")
#Add point as a column to dd so you can add it as an aesthetic
dd$point <- ifelse(dd$dates == "2018-07-02", "Center Point", "Other Points")
ggplot(dd, aes(dates, NB, colour = point, shape = point)) +
geom_segment(aes(xend = dd[point == "Center Point","dates"],
yend = dd[point == "Center Point","NB"]),
colour = "black") +
geom_point() +
ylim(-100,150) +
ylab("Normal Baseline [m]") +
xlab("") +
scale_colour_manual("", values = c("Center Point" = "red", "Other Points" = "black")) +
scale_shape_manual("", values = c("Center Point" = 1, "Other Points" = 2)) +
theme_linedraw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(.8, .8)) +
scale_x_date(breaks = dd$dates)
由 reprex package (v0.3.0)
于 2020-05-04 创建