Smoothing geom_path() with scale_y_reverse() and stat_smooth() - r

My dataset:
reading,depth
31055,0.0
57635,0.5
34268,1.0
20926,1.5
13507,2.0
12944,2.5
13002,3.0
12892,3.5
12610,4.0
12158,4.5
12004,5.0
I'm plotting an instrument reading as a function of below-ground depth. Because of what I'm visualizing, I want the axes to have a non-standard configuration, like this:
ggplot(data=f0101, aes(x=reading, y=depth)) +
theme_classic() +
scale_y_reverse() +
scale_x_continuous(position="top") +
geom_path()
This is perfect, except that I'd like the line smoothed, via stat_smooth() or similar. But, calling a smooth appears to throw off geom_path().
ggplot(data=f0101, aes(x=reading, y=depth)) +
theme_classic() +
scale_y_reverse() +
scale_x_continuous(position="top") +
geom_path() +
stat_smooth()
What can I do to get the smoothing effect of stat_smooth() while still plotting the data in an admittedly non-standard configuration?
Thank you!

I may be wrong, but I think you probably want something like this:
dat %>%
ggplot(aes(x = depth, y = reading)) +
geom_path() +
stat_smooth() +
theme_classic() +
scale_x_reverse() +
scale_y_continuous(position = "top") +
coord_flip()
which produces
For this, you're running the smooth with depth on the x and reading on the y, then flipping the coordinates.

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

Legends in ggplot

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.

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) +
...

Apply coord_flip() to single layer

I would like to have a boxplot showing the same distribution underneath my histogram.
The code below almost works, but coord_flip() is being applied to all layers, instead of just the geom_boxplot layer.
plot1<-ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation])) +
xlab(GGVar) + ylab("Proportion of Instances") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white",origin=-0.5) +
scale_x_continuous(limits=c(-3,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
geom_boxplot(aes_string(x=-1, y=newdatahistogram[RawLocation])) + coord_flip()
How can I apply coord_flip() to a single layer?
Thank you!
I got it to work with a bit of a hack;
plot1 <- ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation], fill=(newdatahistogram[,"PQ"]))) +
xlab(GGVar) + ylab("Proportion of Observation") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", origin=-0.5) +
scale_x_continuous(limits=c(-1,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
scale_y_continuous(limits=c(-.2,1), breaks=seq(0,1,by=.2))
theme(plot.margin = unit(c(0,0,0,0), "cm"))
plot_box <- ggplot(newdatahistogram) +
geom_boxplot(aes_string(x=1, y=newdatahistogram[RawLocation])) +
scale_y_continuous(breaks=(0:5), labels=NULL, limits=c(-1,6), expand=c(.0,-.03)) +
scale_x_continuous(breaks=NULL) + xlab(NULL) + ylab(NULL) +
coord_flip() + theme_bw() +
theme(plot.margin = unit(c(0,0,.0,0), "cm"),
line=element_blank(),text=element_blank(),
axis.line = element_blank(),title=element_blank(), panel.border=theme_blank())
PB = ggplotGrob(plot_box)
plot1 <- plot1 + annotation_custom(grob=PB, xmin=-1.01, xmax=5.95, ymin=-.3,ymax=0)
This saves the rotated boxplot as a grob object and inserts it into the plot under the histogram.
I needed to play with the expansion element a bit to get the scales to line up,
but it works!
Seriously though, I think ggplot should have a horizontal boxplot available without cord_flip()... I tried to edit the boxplot code, but it was way too difficult for me!
Tried to post image, but not enough reputation
You can't: coord_flip always acts on all layers. However, you do have two alternatives:
The solution here shows how to use grid.arrange() to add a marginal histogram. (The comments in the question also link to a nice base-R way to do the same thing)
You could indicate density using a rug plot on of the four sides of the plot with plot1 + geom_rug(sides='r')
ggplot(mpg, aes(x=class, y=cty)) +
geom_boxplot() + geom_rug(sides="r")

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