Legends in ggplot - r

I am trying plot this data, and the line graph is correct, but I can't make the legend show up. Any thoughts?
ggplot(data, aes(x=time_months, size=I(1))) +geom_line(aes(y=monthly_net_revenue, color=I("blue"))) +
geom_line(aes(y=cumsum(discounted_monthly_net_revenue), color=I("purple"))) +
geom_line(aes(y=monthly_expenses, color=I("red"))) +
geom_line(aes(y=cumsum(monthly_revenue), color=I("green")))

This will probably work for you
ggplot(data, aes(x=time_months, size=I(1))) +
geom_line(aes(y=monthly_net_revenue, color="blue")) +
geom_line(aes(y=cumsum(discounted_monthly_net_revenue), color="purple")) +
geom_line(aes(y=monthly_expenses, color="red")) +
geom_line(aes(y=cumsum(monthly_revenue), color="green")) +
scale_color_identity(guide = "legend")
The scale_color_identity() uses the values you pass to color= directly as the color rather than treating them like a group name. You don't need I() with this method.

Related

line colour in ggplot2

I don't understand the following: Why does
data_ts <- data.frame(
day = as.Date("2017-06-14") - 0:364,
value = runif(365) + seq(-140, 224)^2 / 10000)
ggplot(data_ts, aes(x=day, y=value)) +
geom_line() +
scale_colour_manual(values = "#ffcc33")
produce a black line? I know, I could use
ggplot(data_ts, aes(x=day, y=value)) +
geom_line(colour = "#ffcc33")
instead, but I'd like to understand why 'scale_colour_manual' does not work in the example above.
The scale_colour_manual function only effects values that are mapped via an aesthetic aes(). Same goes for all scale_* functions. If values aren't set inside the aes(), then the scale won't touch them. If you wanted to use scale_colour_manual, it would need a mapping. Something like
ggplot(data_ts, aes(x=day, y=value)) +
geom_line(aes(color="mycolor")) +
scale_colour_manual(values = "#ffcc33")
or to ensure a correct match up between mapped literal values and colors, you can do something like
ggplot(data_ts, aes(x=day, y=value)) +
geom_line(aes(color="mycolor1")) +
geom_line(aes(y=value+1, color="mycolor2")) +
scale_colour_manual(values = c(mycolor1="#ffcc33", mycolor2="#33ccff"))

fill and scale_color in ggplot

I am trying to color bars in ggplot but having issues. Can someone explain how to correctly use the fill parameter and the scale_colour parameters?
library(ggplot2)
df<-data.frame(c(80,33,30),c("Too militarized","Just doing their job","Unfairly tarnished by a few"),c("57%","23%","21%"))
colnames(df)<-c("values","names","percentages")
ggplot(df,aes(names,values))+
geom_bar(stat = "identity",position = "dodge",fill=names)+
geom_text(aes(label=percentages), vjust=0)+
ylab("percentage")+
xlab("thought")+
scale_colour_manual(values = rainbow(nrow(df)))
Working barplot example
barplot(c(df$values),names=c("Too militarized","Just doing their job","Unfairly tarnished by a few"),col = rainbow(nrow(df)))
The main issue is that you don't have fill inside a call to aes in geom_bar(). When mapping from data to visuals like colors, it has to be inside aes(). You can fix this by either wrapping fill=names with aes() or by just specifying fill colors directly, instead of using names:
Option 1 (no legend):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", fill=rainbow(nrow(df))) +
ylab("percentage") +
xlab("thought")
Option 2 (legend, because mapping from data to colors):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", aes(fill=names)) +
ylab("percentage") +
xlab("thought") +
scale_fill_manual(values=rainbow(nrow(df)))
Note that in both cases you might want to explicitly factor df$names ahead of the call to ggplot in order to get the bars in the order you want.

ggplot2 facet_wrap doesn't find a variable but shape does

I'm running in a bit of a problem plotting some data with ggplot2: I want to use a facet_wrap over a variable AdultInputProp, but R doesn't find the variable and instead returns an Error in as.quoted(facets) : object 'AdultInputProp' not found. Now I understand that this simply means that R can't find this variable in the dataset used to plot, but if I ask ggplot2 to instead use the same variable for to create a shape scale, it works just fine. Any idea what the problem might be?
Sorry, I'm not too sure how to make a minimal working example with a generated df from scratch, so here's the df I'm using, and the code bellow. I've also tried using facet_grid instead of facet_wrap but ran into the same problem.
The code here with facets returns the above-mentioned error:
df.plot.GBPperAIP <- ggplot(df.sum.GBPperAIP,
aes(x=TestIteration, y=Error,
colour=GoalBabblingProp,
group=interaction(GoalBabblingProp,
AdultInputProp))) +
facet_wrap(AdultInputProp) +
xlab("Step") + ylab("Mean error") + theme_bw(base_size=18) +
scale_colour_discrete(name = "Goal babbling proportion") +
geom_line(position = position_dodge(1000)) +
geom_errorbar(aes(ymin=Error-ci,
ymax=Error+ci),
color="black", width=1000,
position = position_dodge(1000)) +
geom_point(position = position_dodge(1000),
size=1.5, fill="white")
This other code, exactly the same except for the facet_wrap line deleted and with shape added works fine:
df.plot.GBPperAIP <- ggplot(df.sum.GBPperAIP,
aes(x=TestIteration, y=Error,
colour=GoalBabblingProp,
shape=AdultInputProp,
group=interaction(GoalBabblingProp,
AdultInputProp))) +
xlab("Step") + ylab("Mean error") + theme_bw(base_size=18) +
scale_colour_discrete(name = "Goal babbling proportion") +
geom_line(position = position_dodge(1000)) +
geom_errorbar(aes(ymin=Error-ci,
ymax=Error+ci),
color="black", width=1000,
position = position_dodge(1000)) +
geom_point(position = position_dodge(1000),
size=1.5, fill="white")
facet_wrap expects a formula, not just a naked variable name. So you should change it to
...
facet_wrap(~ AdultInputProp) +
...

ggplot2: coloured vertical lines

I'd like to create a ggplot graph with vertical lines of different colors. Here one way to achieve this goal.
mtcars$colors = rep(1:4, nrow(mtcars)/4)
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_vline(xintercept=subset(mtcars, colors==1)$wt, color="red") +
geom_vline(xintercept=subset(mtcars, colors==2)$wt, color="blue") +
geom_vline(xintercept=subset(mtcars, colors==3)$wt, color="yellow") +
geom_vline(xintercept=subset(mtcars, colors==4)$wt, color="green")
This solution is not very handy when the variable colors takes 50 different values 1) because it asks the user to write a very long expression (or to construct the ggplot object iteratively) and 2) because it does not produce legends for the colors. Is there a better solution?
Maybe this instead:
+ geom_vline(aes(xintercept = wt,color = factor(colors))) +
scale_color_manual(values = c('red','blue','yellow','green'))

How to format number values for ggplot2 legend?

I am working on finishing up a graph generated using ggplot2 like so...
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue))
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)
I have attached the result and you can see the numeric values in the legend for Revenue do not have a comma. How can I add a comma to those values? I was able to use scale_y_continuous for the axis, can that be used for the legend also?
Just to keep current, in ggplot2_0.9.3 the working syntax is:
require(scales)
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(labels=comma) +
scale_colour_continuous(labels=comma)
Also see this exchange
Note 2014-07-16: the syntax in this answer has been obsolete for some time. Use metasequoia's answer!
Yep - just a matter of getting the right scale_colour_ layer figured out. Try:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors, colour = Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
I personally would also move my the colour mapping to the geom_point layer, so that it doesn't give you that odd line behind the dot in the legend:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors)) +
geom_point(aes(colour = Revenue)) +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
...as I stumbled over this older thread, maybe it makes sense to add you need to load library("scales"), otherwise you get the following error message
Error in check_breaks_labels(breaks, labels) : object 'comma' not found

Resources