Editing labels (text) labels in ggplot

I spent hours looking at the documentation and StackOverflow, but it seems that not a single solution has solved my problem. When using ggplot I cannot get the correct text in the legend, even if it is in my data frame. I tried scale_colour_manual , scale_fill_manual with different values ​​for labels= such as c("T999", "T888")", "cols" .

Here is my code:

 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") 

Help will be much appreciated!

+100
text r ggplot2 label
May 13 '14 at 15:36
source share
2 answers

The @Henrik tutorial mentioned above is a great resource for learning how to create graphs with ggplot2 ggplot2 .

An example with your data:

 # 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.y = element_text(size = 16), plot.title = element_text(size = 20, face = "bold", color = "darkgreen")) 

it leads to:

enter image description here

As @ user2739472 mentioned in the comments: if you want to change only the legend text labels and not the colors from the ggplot palette by default, you can use scale_color_hue(labels = c("T999", "T888")) instead of scale_color_manual()

+105
May 13 '14 at 17:14
source share

Legend names can be labeled with a certain aesthetic.

This can be achieved using the guides() or labs() ggplot2 from ggplot2 (more details here and here ). This allows you to add guide / legend properties with an aesthetic display.

Here is an example using the mtcars and labs() mtcars :

 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") 

enter image description here

Answering an OP question with 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 

enter image description here

+31
Jul 20 '16 at 16:29
source share



All Articles