Edición de etiquetas de leyenda (texto) en ggplot
He pasado horas buscando en la documentación y en StackOverflow, pero ninguna solución parece resolver mi problema. Al usar ggplot
no puedo obtener el texto correcto en la leyenda, aunque esté en mi dataframe. He intentado scale_colour_manual
, scale_fill_manual
con diferentes valores para labels=
como c("T999", "T888")", "cols"
.
Aquí está mi código:
T999 <- runif(10, 100, 200)
T888 <- runif(10, 200, 300)
TY <- runif(10, 20, 30)
df <- data.frame(T999, T888, TY)
ggplot(data = df, aes(x=T999, y=TY, pointtype="T999")) +
geom_point(size = 15, colour = "darkblue") +
geom_point(data = df, aes(x=T888, y=TY), colour = 'red', size = 10 ) +
theme(axis.text.x = element_text(size = 20), axis.title.x =element_text(size = 20), axis.text.y = element_text(size = 20)) +
xlab("Txxx") + ylab("TY [°C]") + labs(title="temperatures", size = 15) +
scale_colour_manual(labels = c("T999", "T888"), values = c("darkblue", "red")) + theme(legend.position="topright")
La ayuda sería muy apreciada! Thx.
2 answers
El tutorial @Henrik mencionado es un excelente recurso para aprender a crear gráficas con el paquete ggplot2
.
Un ejemplo con sus datos:
# transforming the data from wide to long
library(reshape2)
dfm <- melt(df, id = "TY")
# creating a scatterplot
ggplot(data = dfm, aes(x = TY, y = value, color = variable)) +
geom_point(size=5) +
labs(title = "Temperatures\n", x = "TY [°C]", y = "Txxx", color = "Legend Title\n") +
scale_color_manual(labels = c("T999", "T888"), values = c("blue", "red")) +
theme_bw() +
theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 14), axis.title.x = element_text(size = 16),
plot.title = element_text(size = 20, face = "bold", color = "darkgreen"))
Esto resulta en:
Como menciona @user2739472 en los comentarios: Si solo desea cambiar las etiquetas de texto de leyenda y no los colores de la paleta predeterminada de ggplot, puede usar scale_color_hue(labels = c("T999", "T888"))
en lugar de scale_color_manual()
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-19 05:02:25
Los títulos de las leyendas se pueden etiquetar con estéticos específicos.
Esto se puede lograr usando las funciones guides()
o labs()
de ggplot2
(más aquí y aquí). Le permite agregar propiedades de guía/leyenda utilizando la asignación estética.
Aquí hay un ejemplo usando el conjunto de datos mtcars
y labs()
:
ggplot(mtcars, aes(x=mpg, y=disp, size=hp, col=as.factor(cyl), shape=as.factor(gear))) +
geom_point() +
labs(x="miles per gallon", y="displacement", size="horsepower",
col="# of cylinders", shape="# of gears")
Respondiendo a la pregunta de la OP usando guides()
:
# transforming the data from wide to long
require(reshape2)
dfm <- melt(df, id="TY")
# creating a scatterplot
ggplot(data = dfm, aes(x=TY, y=value, color=variable)) +
geom_point(size=5) +
labs(title="Temperatures\n", x="TY [°C]", y="Txxx") +
scale_color_manual(labels = c("T999", "T888"), values = c("blue", "red")) +
theme_bw() +
guides(color=guide_legend("my title")) # add guide properties by aesthetic
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-20 17:36:33