R:如何将图例添加到具有5个统计差异的散点图中
我是R领域的新手,我正在尝试用ggplot2制作一个图形,在这里你可以获得5种不同的统计数据。所以,我想在右边有一个带有体育场馆颜色和名称的图例,但我不知道怎么做 我制作图形的代码是:R:如何将图例添加到具有5个统计差异的散点图中,r,ggplot2,legend,R,Ggplot2,Legend,我是R领域的新手,我正在尝试用ggplot2制作一个图形,在这里你可以获得5种不同的统计数据。所以,我想在右边有一个带有体育场馆颜色和名称的图例,但我不知道怎么做 我制作图形的代码是: ggplot(object@data, aes(x=data[,1])) + geom_point(aes(y=data[,2]), color="blue") + geom_point(aes(y=data[,3]), color = "red") + geom_point(aes
ggplot(object@data, aes(x=data[,1])) +
geom_point(aes(y=data[,2]), color="blue") +
geom_point(aes(y=data[,3]), color = "red") +
geom_point(aes(y=data[,4]), color = "olivedrab4") +
geom_point(aes(y=data[,5]), color = "hotpink4") +
geom_point(aes(y=data[,6]), color = "limegreen") +
labs(title = "Liga DIA: Comparativas (Liga Regular) - www.basketmetrics.com",
x = "Equipos", y = "Total") +
#Pone las etiquetas del eje de las X en vertical
theme(axis.text.x = element_text(angle = 90, hjust = 1, color = "white"),
axis.text.y = element_text(color = "white"),
legend.position = "top", legend.title = element_blank()) +
#Modificamos el fondo del panel
theme(panel.background = element_rect(fill = 'peachpuff', colour = 'white')) +
theme(plot.background = element_rect(fill = 'navyblue', colour = 'white'),
plot.title = element_text(size = 20, face = "bold", color = "white"),
axis.title.x = element_text(face = "bold", color = "white"),
axis.title.y = element_text(face = "bold", color = "white")) +
scale_color_manual(name="Estadísticas", labels = c("ORTG", "DRTG", "Pace", "eFG%", "3P%"),
values = c("blue", "red", "olivedrab4", "hotpink4", "limegreen"))
我以为有了这段代码,我就可以拥有传奇,但这对我不起作用:(
现在,我得到的是这个图形:
如何在每种颜色和统计名称的右侧设置图例?您可以从以下步骤开始。
正如@Jon Spring所述,
ggplot2
更喜欢数据的宽格式,因此首先应该转换数据,而不是应用ggplot2
函数
# you can use this package, to melt the data. You have not given any data, so
# at the bottom of this question, you can see some fake data similar to your:
library(reshape2)
long <- melt(data, id.vars = c("name"))
long
name variable value
1 a col2 2
2 b col2 2
3 c col2 2
4 a col3 3
5 b col3 3
6 c col3 3
7 a col4 4
8 b col4 4
9 c col4 4
10 a col5 5
11 b col5 5
12 c col5 5
13 a col6 6
14 b col6 6
15 c col6 6
关于数据:
data <- data.frame(name = c('a','b','c'),
col2 = c(2,2,2),
col3 = c(3,3,3),
col4 = c(4,4,4),
col5 = c(5,5,5),
col6 = c(6,6,6)
)
数据您可以获得具有映射美学的图例。因此,映射美学!我如何才能做到这一点?请先阅读任何基本的ggplot
教程。您需要首先“收集”绘制的列,将它们从5个单独的列移动到2个列,其中一个列说明它是哪一列(“键”)还有一个显示值。然后你把这个输入到ggplot,在那里,关键点被映射到一种美学,比如颜色。下面是这两个步骤的一个例子:非常感谢hoy@s\t的回答!!!
ggplot(long, aes(x=long[,1],y =long[,3], color = long[,2])) +
# here only one geom_point, using the long format
geom_point() +
labs(title = "Liga DIA: Comparativas (Liga Regular) - www.basketmetrics.com", x = "Equipos", y = "Total") +
# you can manage the theme features in only one theme
theme(axis.text.x = element_text(angle = 90, hjust = 1, color = "white"),
axis.text.y = element_text(color = "white"),
legend.position = "top", legend.title = element_blank(),
panel.background = element_rect(fill = 'peachpuff', colour = 'white'),
plot.background = element_rect(fill = 'navyblue', colour = 'white'),
plot.title = element_text(size = 20, face = "bold", color = "white"),
axis.title.x = element_text(face = "bold", color = "white"),
axis.title.y = element_text(face = "bold", color = "white"),
legend.key = element_rect(fill = "transparent", colour = "transparent")) +
scale_color_manual(name="Estadísticas", labels = c("ORTG", "DRTG", "Pace", "eFG%", "3P%"),values = c("blue", "red", "olivedrab4", "hotpink4", "limegreen"))
data <- data.frame(name = c('a','b','c'),
col2 = c(2,2,2),
col3 = c(3,3,3),
col4 = c(4,4,4),
col5 = c(5,5,5),
col6 = c(6,6,6)
)