如何在散点图中绘制不同的周末?
How to plot the weekend days in a scatterplot as different?
我有以下数据table(仅举个例子)dt.data
:
dt.data <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
DE = rnorm(365, 4, 1), AT = rnorm(365, 10, 2),
IT = rnorm(365, 1, 2), check.names = FALSE)
# Add nr-column for different colored points: #
dt.data$nr <- sort(rep(1:7, length.out = nrow(dt.data)))
## PLOT: ##
p <- ggplot(data = dt.data, aes(x = AT, y = DE, color = as.factor(nr),
text = paste("Date: ", date, '\n',
"AT: ", AT, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)
) +
geom_point() +
scale_color_manual(values = colorRampPalette(brewer.pal(n = 8, name = "Greens")[4:8])( length(unique(dt.allData$nr)) )) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("AT") +
ylab("DE")
# Correlation plot converting from ggplot to plotly: #
scatterPlot <- plotly::ggplotly(p, tooltip = "text")
因此,我得到以下情节:
我想要一周(周一到周五)的数据点,因为 点 代表(这里所有)和周六和周日的数据点(也可能是国家假期)用 cross/plus/triangle 表示。我该怎么做?
尝试用下一种方式为 weekend/weekday 创建因子变量,并在 geom_point()
中启用 shape
选项:
#Create day of week
dt.data$Day <- as.numeric(weekdays(dt.data$date) %in% c('Saturday','Sunday'))
dt.data$Day <- factor(dt.data$Day,levels = c(1,0),labels = c('Weekend','Weekday'))
## PLOT: ##
p <- ggplot(data = dt.data, aes(x = AT, y = DE, color = as.factor(nr),
text = paste("Date: ", date, '\n',
"AT: ", AT, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)) +
geom_point(aes(shape=Day)) +
scale_color_manual(values = colorRampPalette(brewer.pal(n = 8, name = "Greens")[4:8])(length(unique(dt.data$nr)))) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("AT") +
ylab("DE")
# Correlation plot converting from ggplot to plotly: #
scatterPlot <- plotly::ggplotly(p, tooltip = "text")
输出将是:
或者遵循 @teunbrand 的明智建议:
## PLOT 2
ggplot(data = dt.data, aes(x = AT, y = DE, color = as.factor(nr),
text = paste("Date: ", date, '\n',
"AT: ", AT, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)) +
geom_point(aes(shape=Day)) +
scale_color_manual(values = colorRampPalette(brewer.pal(n = 8, name = "Greens")[4:8])(length(unique(dt.data$nr)))) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("AT") +
ylab("DE") +
scale_shape_manual(values = c('Weekend'=1,'Weekday'=3))
输出:
我有以下数据table(仅举个例子)dt.data
:
dt.data <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
DE = rnorm(365, 4, 1), AT = rnorm(365, 10, 2),
IT = rnorm(365, 1, 2), check.names = FALSE)
# Add nr-column for different colored points: #
dt.data$nr <- sort(rep(1:7, length.out = nrow(dt.data)))
## PLOT: ##
p <- ggplot(data = dt.data, aes(x = AT, y = DE, color = as.factor(nr),
text = paste("Date: ", date, '\n',
"AT: ", AT, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)
) +
geom_point() +
scale_color_manual(values = colorRampPalette(brewer.pal(n = 8, name = "Greens")[4:8])( length(unique(dt.allData$nr)) )) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("AT") +
ylab("DE")
# Correlation plot converting from ggplot to plotly: #
scatterPlot <- plotly::ggplotly(p, tooltip = "text")
因此,我得到以下情节:
我想要一周(周一到周五)的数据点,因为 点 代表(这里所有)和周六和周日的数据点(也可能是国家假期)用 cross/plus/triangle 表示。我该怎么做?
尝试用下一种方式为 weekend/weekday 创建因子变量,并在 geom_point()
中启用 shape
选项:
#Create day of week
dt.data$Day <- as.numeric(weekdays(dt.data$date) %in% c('Saturday','Sunday'))
dt.data$Day <- factor(dt.data$Day,levels = c(1,0),labels = c('Weekend','Weekday'))
## PLOT: ##
p <- ggplot(data = dt.data, aes(x = AT, y = DE, color = as.factor(nr),
text = paste("Date: ", date, '\n',
"AT: ", AT, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)) +
geom_point(aes(shape=Day)) +
scale_color_manual(values = colorRampPalette(brewer.pal(n = 8, name = "Greens")[4:8])(length(unique(dt.data$nr)))) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("AT") +
ylab("DE")
# Correlation plot converting from ggplot to plotly: #
scatterPlot <- plotly::ggplotly(p, tooltip = "text")
输出将是:
或者遵循 @teunbrand 的明智建议:
## PLOT 2
ggplot(data = dt.data, aes(x = AT, y = DE, color = as.factor(nr),
text = paste("Date: ", date, '\n',
"AT: ", AT, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)) +
geom_point(aes(shape=Day)) +
scale_color_manual(values = colorRampPalette(brewer.pal(n = 8, name = "Greens")[4:8])(length(unique(dt.data$nr)))) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("AT") +
ylab("DE") +
scale_shape_manual(values = c('Weekend'=1,'Weekday'=3))
输出: