[英]ggplot2 legend not working/ add manual legend
我是 R 的新手。 图例和绘图似乎比 Python 更难。 如何更改图表以在图例中将每个节点显示为不同的颜色? 现在我有类似图片的东西。 谢谢您的帮助。
library(ggplot2)
library(MASS)
library(car)
library(robustbase)
data("airquality")
# Select only Ozone and Temp variables
air = airquality[c("Ozone" , "Temp")]
# We need to remove NA from data set
air = na.omit(air)
air.center = colMeans(air)
air.cov = cov(air)
rad = sqrt(qchisq(p = 0.95 , df = ncol(air)))
ellipse <- ellipse(center = air.center , shape = air.cov , radius = rad ,
segments = 150 , draw = FALSE)
ellipse <- as.data.frame(ellipse)
colnames(ellipse) <- colnames(air)
# Finding distances
distances <- mahalanobis(x = air , center = air.center , cov = air.cov)
# Cutoff value for ditances from Chi-Sqaure Dist.
# with p = 0.95 df = 2 which in ncol(air)
cutoff <- qchisq(p = 0.95 , df = ncol(air))
### Minimum Covariance Determinant (MCD)
Y_mcd <- covMcd(air)
# Robust estimate of location
Y_mcd$center
# Robust estimate of scatter
Y_mcd$cov
# Make elilipse
ellipse_mcd <- data.frame(ellipse(center = Y_mcd$center,
shape = Y_mcd$cov,
radius= rad,
segments=100,draw=FALSE))
#the same names as in previous plot
colnames(ellipse_mcd) <- colnames(air)
plot_fig <- ggplot(air , aes(x = Ozone , y = Temp)) +
geom_point(size = 2) +
geom_polygon(data = ellipse , fill = "blue" , color = "blue" , alpha = 0.5,show.legend =T)+
geom_point(aes(air.center[1] , air.center[2],fill='Mahalanobis') , size = 5 , color = "blue") +
geom_text(data=subset(air, distances > cutoff),
aes(Ozone,Temp,label=row.names(air[distances > cutoff ,])), hjust = 1 , vjust = -1.5 ,size = 3.5)+
ylab("Temperature Values") + xlab("Ozone Values")+ggtitle("Mahalanobis distance")+ theme(
legend.position = c(0.95, 0.15),
legend.justification = c("right", "top")
) + geom_polygon(data=ellipse_mcd,aes(x = Ozone,y = Temp, colour='LINE2'), color="red", fill="red",
alpha=0.3, inherit.aes = FALSE) +
geom_point(aes(x = Y_mcd$center[1], y = Y_mcd$center[2],fill='MCD'),
color = "red", size = 6)
plot_fig
问题是您映射了fill
美学并将点的颜色设置为 arguments。 而是 map color
aes 上的标签并通过scale_color_manual
设置颜色值:
plot_fig <- ggplot(air , aes(x = Ozone , y = Temp)) +
geom_point(size = 2) +
geom_polygon(data = ellipse , fill = "blue" , color = "blue" , alpha = 0.5, show.legend =T)+
geom_point(aes(air.center[1] , air.center[2], color = 'Mahalanobis'), size = 5) +
geom_text(data=subset(air, distances > cutoff),
aes(Ozone,Temp,label=row.names(air[distances > cutoff ,])), hjust = 1 , vjust = -1.5 ,size = 3.5)+
ylab("Temperature Values") +
xlab("Ozone Values")+
ggtitle("Mahalanobis distance")+
theme(
legend.position = c(0.95, 0.15),
legend.justification = c("right", "top")
) +
geom_polygon(data=ellipse_mcd,aes(x = Ozone,y = Temp, colour='LINE2'), color="red", fill="red",
alpha=0.3, inherit.aes = FALSE) +
geom_point(aes(x = Y_mcd$center[1], y = Y_mcd$center[2], color='MCD'), size = 6) +
scale_color_manual(values = c(MCD = "red", Mahalanobis = "blue"))
plot_fig
如果您将 Mahalanobis/MCD 分类变量 map 设置为colour
,则让fill
取决于映射的颜色,图例应该自然地自行排序,您可以使用scale_colour_manual()
设置颜色。
ggplot(air, aes(Ozone, Temp)) +
geom_point(size = 2) +
geom_polygon(
data = ellipse,
aes(fill = after_scale(alpha(colour, 0.5)), colour = "Mahalanobis")
) +
geom_polygon(data = ellipse_mcd,
aes(fill = after_scale(alpha(colour, 0.3)), colour = "MCD")) +
geom_point(aes(air.center[1], air.center[2], colour = "Mahalanobis"),
size = 5) +
geom_point(aes(Y_mcd$center[1], Y_mcd$center[2], colour = "MCD"), size = 5) +
geom_text(data = subset(air, distances > cutoff),
aes(label = row.names(air[distances > cutoff, ])),
hjust = 1, vjust = -1.5, size = 3.5) +
scale_colour_manual(values = c("blue", "red")) +
labs(x = "Ozone Values", y = "Temperature values",
title = "Mahalanobis distance") +
theme(legend.position = c(0.95, 0.15),
legend.justification = c("right", "top"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.