Shading lines ggplot2 - r

Does anyone know how to add shading lines in an area or histogram plot in ggplot2?
For example, How could I reproduce the following plot in ggplot2 ?
hist(mtcars$hp, density = 10)
Thanks.

Related

Log scale for contour plot in Plots.jl

Is it possible to use a log scale in a contour plot? The plot should look like contour(log10(data)), but with correct labelling of the colorbar.

how to plot countour curve on top of scatter plot in Octave

I am using Octave plot() function to plot scatter points on a 2D graph. And then I am using the contour() function to draw a contour on top of the points. But the contour() function is not overlapping on top of the points. What happens is that the scatter plot graph is replaced entirely with the contour, even though I am using the HOLD ON command.
I have something like this:
plot(); %plot the x,y scatter plot
hold on; %hold on to be able to add to the plot
contour(); %Add the contour on top of the scatter plot
I wonder if someone can show some sample code they can show to add a contour to an existing plot.
thanks
Here is an example :
x = [-10:0.1:10];
y = x .^ 2;
z = x' * x;
hold on;
contour(x,y,z);
plot(x,y);
Will yield this graph (in blue you can see the parabol issued by plot).

Add a curve to a log/log scatterplot

I have a scatterplot in a log/log space
plot(a,b,log="xy")
or in ggplot2
qplot(a,b,data="time",log="xy")
Now I would like to impose upon this scatter plot the curve f(x)=x*x+2. Butthe function woudl need to be plotted in the logarithmic space as well. How would I do this? Is there an way to do this in ggplot2?
As you guessed, curve is the command that you're looking for in base graphics.
#Make up some data
set.seed(0)
a <- 1:10
b <-(a^2+2)*exp(0.1*rnorm(10))
plot(a,b,log='xy')
curve(x^2+2,add=TRUE)
in ggplot2 world:
qplot(a,b,data=time)+stat_function(fun=function(x){x^2+2}) + coord_trans(xtrans = "log10",ytrans="log10")
from Plotting in R using stat_function on a logarithmic scale seems to do what you're after.

Density scatter plot with legend

Following-up on a question that has already been answered regarding the scatterplot density (see R Scatter Plot: symbol color represents number of overlapping points), I would like to know if there is a way to add the legend of the scatterplot, let's say for instance to the solution proposed by O'brien?

R - add plot to facetted ggplot2 object (using annotation_custom)

I'm trying to inset a plot using ggplot2 and annotation_custom (the plot is actually a map that I'm using to replace the legend). However, I'm also using facet_wrap to generate multiple panels, but when used with annotation_custom, this reproduces the plot in each facet. Is there an easy way to insert the plot only once, preferably outside the plotting area?
Here is a brief example:
#Generate fake data
set.seed(9)
df=data.frame(x=rnorm(100),y=rnorm(100),facets=rep(letters[1:2]),
colors=rep(c("red","blue"),each=50))
#Create base plot
p=ggplot(df,aes(x,y,col=colors))+geom_point()+facet_wrap(~facets)
#Create plot to replace legend
legend.plot=ggplotGrob(
ggplot(data=data.frame(colors=c("red","blue"),x=c(1,1),y=c(1,2)),
aes(x,y,shape=colors,col=colors))+geom_point(size=16)+
theme(legend.position="none") )
#Insert plot using annotation_custom
p+annotation_custom(legend.plot)+theme(legend.position="none")
#this puts plot on each facet!
This produces the following plot:
When I would like something more along the lines of:
Any help is appreciated. Thanks!
In the help of annotation_custom() it is said that annotations "are the same in every panel", so it is expected result to have your legend.plot in each facet (panel).
One solution is to add theme(legend.position="none") to your base plot and then use grid.arrange() (library gridExtra) to plot both plots.
library(gridExtra)
p=ggplot(df,aes(x,y,col=colors))+geom_point()+facet_wrap(~facets)+
theme(legend.position="none")
grid.arrange(p,legend.plot,ncol=2,widths=c(3/4,1/4))

Resources