Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
184 votes
4 answers
406k views

Add legend to ggplot2 line plot

I have a question about legends in ggplot2. I managed to plot three lines in the same graph and want to add a legend with the three colors used. This is the code used library(ggplot2) ## edit from ...
pacomet's user avatar
  • 5,111
200 votes
8 answers
73k views

Remove 'a' from legend when using aesthetics and geom_text

How can I can remove the letter 'a' from the legend generated by this code? If I remove the geom_text, then the 'a' letter will not show in the legend. I want to keep geom_text, though. ggplot(data = ...
user2700207's user avatar
  • 2,043
116 votes
10 answers
164k views

Plot labels at ends of lines

I have the following data (temp.dat see end note for full data) Year State Capex 1 2003 VIC 5.356415 2 2004 VIC 5.765232 3 2005 VIC 5.247276 4 2006 VIC 5.579882 5 2007 VIC ...
Hugh's user avatar
  • 15.9k
212 votes
9 answers
271k views

Add a common Legend for combined ggplots

I have two ggplots which I align horizontally with grid.arrange. I have looked through a lot of forum posts, but everything I try seem to be commands that are now updated and named something else. ...
jO.'s user avatar
  • 3,472
130 votes
4 answers
56k views

Keep unused levels in bar plot

I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them df <- data.frame(type=c("A", "A", "A"...
Ulrik's user avatar
  • 1,625
10 votes
2 answers
5k views

How to set multiple legends / scales for the same aesthetic in ggplot2?

I am plotting data from multiple dataframes in ggplot2 as follows: # subset of iris data vdf = iris[which(iris$Species == "virginica"),] # plot from iris and from vdf ggplot(iris) + ...
user avatar
187 votes
9 answers
194k views

Is there a way to change the spacing between legend items in ggplot2?

Is there a way to change the spacing between legend items in ggplot2? I currently have legend.position ="top" which automatically produces a horizontal legend. However, the spacing of the items is ...
overflowname's user avatar
  • 2,153
79 votes
1 answer
39k views

Control ggplot2 legend look without affecting the plot

I'm plotting lines with ggplot2 like this: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw() . I find legend marks to be small so I want them to be bigger. If I ...
baltazar's user avatar
  • 1,679
38 votes
6 answers
45k views

ggplot2: Divide Legend into Two Columns, Each with Its Own Title

I have these factors require(ggplot2) names(table(diamonds$cut)) # [1] "Fair" "Good" "Very Good" "Premium" "Ideal" which I want to ...
CptNemo's user avatar
  • 6,665
31 votes
3 answers
21k views

How do I position two legends independently in ggplot

Title pretty well covers it. I have two legends, relating to size and colour, and wish to have one,say, on the top and one within the graph. Is this possible and, if so, how TIA
pssguy's user avatar
  • 3,505
133 votes
4 answers
151k views

Remove extra legends in ggplot2

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this: df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20), ...
Wilduck's user avatar
  • 14k
34 votes
5 answers
29k views

Place a legend for each facet_wrap grid in ggplot2

I have this data frame: x <- data.frame( Date = factor(rep( c("12/1/2011", "1/2/2012", "2/1/2012", "2/10/2012", "2/13/2012"), 3 )), ...
user1471980's user avatar
  • 10.5k
376 votes
4 answers
604k views

Remove legend ggplot 2.2

I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried shutting off the legends with guides(colour = FALSE) and geom_point(aes(color = vs), show....
Guy's user avatar
  • 3,785
31 votes
1 answer
31k views

Missing legend with ggplot2 and geom_line

How does one get a legend to display when plotting lines in ggplot? I've been trying all evening but have been unsuccessful. p <- ggplot(output, aes(lambda), legend=TRUE) + geom_line(aes(y=...
MrEvil's user avatar
  • 8,013
15 votes
2 answers
19k views

ggplot separate legend and plot

I am using the grid lpackage to place my graphs that I made with ggplot2: library(ggplot2) library(grid) Layout <- grid.layout(nrow = 4, ncol = 4, widths = unit(1, "null"), ...
user1690049's user avatar

15 30 50 per page
1
2 3 4 5
19