Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
edited by

I have spent hours looking at the documentation, but no solution seems to solve my problem. When using ggplot I can't get the right text in the legend, even though it's in my data frame. I have 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 would be very appreciated!

1 Answer

0 votes
by
edited by

To plot with legend text labels, you need to follow the steps below:

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=3) +

 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 = 11), axis.title.x = element_text(size = 12), axis.text.y = element_text(size = 11), axis.title.y = element_text(size = 14), plot.title = element_text(size = 15, face = "bold", color = "darkgreen"))

Output:

image

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Jul 4, 2019 in R Programming by leealex956 (7.3k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...