All Questions
280
questions
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 ...
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 = ...
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 ...
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.
...
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"...
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) +
...
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 ...
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 ...
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 ...
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
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),
...
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
)),
...
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....
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=...
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"),
...