R stat_smooth all points - r

From Plot vectors of different length with ggplot2, I've got my plot with lines.
ggplot(plotData, aes(x, y, label=label, group=label)) + geom_line() + stat_smooth()
But this smooths one line each. How do I smooth over all data points?

ggplot(plotData, aes(x, y, label=label, group=label)) +
geom_line() +
geom_smooth(aes(group = 1))
should do it. The idea here is to provide a new group aesthetic so that the fitted smoother is based on all the data, not the group = label aesthetic.
Following the example from #Andrie's Answer the modification I propose would be:
ggplot(plotData, aes(x, y, label=label, group=label)) +
geom_text() +
geom_smooth(aes(group = 1))
which would produce:

Related

How to flip a geom_area to be under the line when using scale_y_reverse()

I had to flip the axis of my line, but still need the geom_area to be under the curve. However I cannot figure out how to do so.
This is the line of code I tried
ggplot(PalmBeachWell, aes(x=Date, y=Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_area(position= "identity", fill='lightblue') +
theme_classic() +
geom_line(color="blue") +
scale_y_reverse()
and here is what i got
One option would be to use a geom_ribbon to fill the area above the curve which after applying scale_y_reverse will result in a fill under the curve.
Using some fake example data based on the ggplot2::economics dataset:
library(ggplot2)
PalmBeachWell <- economics[c("date", "psavert")]
names(PalmBeachWell) <- c("Date", "Depth.to.Water.Below.Land.Surface.in.ft.")
ggplot(PalmBeachWell, aes(x = Date, y = Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_ribbon(aes(ymin = Depth.to.Water.Below.Land.Surface.in.ft., ymax = Inf),
fill = "lightblue"
) +
geom_line(color = "blue") +
scale_y_reverse() +
theme_classic()

Changing legend title in ggplot changes the shown legend aesthetic

I make some plot with ggplot2 like this:
library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
geom_point()
Everything is good but as soon I set another legend title as shown here, the continuous colour gradient changes to some points in the legend:
library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
geom_point() +
guides(col= guide_legend(title= "Some title"))
How can I change just the title not the eastethic in the legend?
Data used here:
df <- data.frame(x= 1:10, y= 1:10, col= rnorm(10))
You need guide_colourbar instead of guide_legend
ggplot(df, aes(x, y, col = col)) +
geom_point() +
guides(col = guide_colourbar(title = "Some title"))
Though personally, I would normally just change the label of the color aesthetic:
ggplot(df, aes(x, y, col = col)) +
geom_point() +
labs(color = "Some title")
Which gives the same result with fewer keystrokes.

ggplot Adding Tracking Colors Below X-Axis

I'd like to add a line below the x-axis where its color is dependant on a factor that is not plotted.
In this example, I'm creating a box plot and would like to add a line that indicates another variable.
Using the cars data set as an example and then physically dawing in what I'm trying to do:
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot()
My thought was to create a bar, column, or geom_tile plot and then arrange it below the boxplot. This is how I would do it in base R. Is there a way to add in these kinds of color labels in ggplot2?
The natural way in ggplot2 to do this sort of thing would to be facet on the categorical variable to create subplots. However if you want to keep everything on the same graph you could try using a geom_tile() layer something like this:
df <-data.frame(x = factor(c(4,6,8)), colour = factor(c(1,2,1)))
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 8, fill = colour))
Alternatively as you suggest you could align an additional plot underneath it. You could use ggarrange() in the ggpubr package for this:
plot1 <- ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 10, fill = colour))
theme(legend.position = 'none')
plot2 <- ggplot(df, aes(x=x, y=1, fill = colour)) +
geom_tile() +
theme_void() +
scale_fill_manual(values=c('orange', 'green', 'orange')) +
theme(legend.position = 'none')
library(ggpubr)
ggarrange(plot1, plot2, nrow = 2, heights = c(10, 1), align = 'h')

box plot in R with additional point

I have a dataframe of multiple columns (let's say n) with different range and a vector of length n. I want different x-axis for each variable to be shown below each box plot. I tried facet_grid and facet_wrap but it gives common x axis.
This is what I have tried:
d <- data.frame(matrix(rnorm(10000), ncol = 20))
point_var <- rnorm(20)
plot.data <- gather(d, variable, value)
plot.data$test_data <- rep(point_var, each = nrow(d))
ggplot(plot.data, aes(x=variable, y=value)) +
geom_boxplot() +
geom_point(aes(x=factor(variable), y = test_data), color = "red") +
coord_flip() +
xlab("Variables") +
theme(legend.position="none")
If you can live with having the text of the x axis above the plot, and having the order of the graphs a bit messed-up this could work:
library(grid)
p = ggplot(plot.data, aes(x = 0, y=value)) +
geom_boxplot() +
geom_point(aes(x = 0, y = test_data), color = "red") +
facet_wrap(~variable, scales = "free_y", switch = "y") +
xlab("Variables") +
theme(legend.position="none") + theme_bw() + theme(axis.text.x=element_blank())
print(p, vp=viewport(angle=270, width = unit(.75, "npc"), height = unit(.75, "npc")))
I'm actually just creating the graph without flipping coords, so that scales = 'free_y' works, swithcing the position of the strip labels, and then rotating the graph.
If you don't like the text above graph (which is understandable), I would consider creating a list of single plots and then putting them together with grid.arrange.
HTH,
Lorenzo

How to get proper shape and color for points in legend when adding geom_smooth?

If I add geom_smooth, then I'm getting rectangles of different color in the shape legend instead of black circles. How can I prevent this? Here is the sample code.
library(ggplot2)
df <- data.frame(x=rnorm(100), y=rnorm(100), z=runif(100))
qplot(x, y, size=z, data=df) +
geom_smooth(method='loess', aes(weight=z))
This is fixed by specifying that the size aesthetic is specific to the point layer:
ggplot(df, aes(x = x, y = y)) +
geom_point(aes(size = z)) +
geom_smooth(method = "loess", aes(weight = z))

Resources