I am trying to combine a scatterplot with a bar plot. The bar plot should be on the right side of the scatterplot of each corresponding group specified in facet wrap. The two plots have the same unit on the y axis but the scaling of the bar might need to be adjusted so the barplot it's not too tall.
Here's an example.
data(mpg)
So far I have tried this, but I would the bar should be outside the scatterplot area. Additionally, in my data the x axis in the barplot is a factor with two levels, so it has a discrete scale and my scatterplot has a continuous x scale.
ggplot()+geom_bar(data=mpg,aes(x=10,y=displ,fill=trans),stat='identity',position='stack')
+geom_point(data=mpg,aes(x=displ,y=displ))+facet_wrap(~cyl)
Can I achieve to plot these two plot types side-by-side or do I have to use something like cowplot? I think that if I have to combine plots outside ggplot I will not be able to use factor_wrap, so I guess I have to do a for loop for each factor level and then combine everything?
I have created a series of marginal plots looking at two variables using ggMarginal and ggplot2. However, I want to include an axis on the histogram that has the scale so I know roughly how many values fall into each bin.
I have already checked the documentation for ggMarginal and am at a loss.
I am trying to recreate a figure similar to the attached plot (similar to the target plot).
I have found certain topics that are similar (e.g. one 3D plot comprising stacked 2D plots (How can I make 3d plot with stacked 2d plot?) or plotting mean for density distributions (plot median values on top of a density distribution in ggplot2)).
But I was unable to find an example in which there is a 3D plot of stacked 2D plots and, in addition, there is a plot of showing the relationship between the mean values of the said stacked 2D plots.
Thanks
This question already has answers here:
Sort legend in ggplot2
(2 answers)
Closed 4 years ago.
I have two stacked area plots with a line plotted on top. In both cases, my plotting order has been this:
Create ggplot
Add stacked area (geom_area); change stacked area colors
Add line (geom_line); change line color
In my first graph, the stacked area colors show up above the Reported Catch line my legend, while in my second graph, the Reported Catch line shows up above the stacked area colors. There appears to be no major difference between the two in terms of R code, so I have no idea why are they different in legend order.
How can I change my first graph's legend to match the second graph's legend, so that the "Reported Catch" line is above the stacked area colors?
Plot 1. Incorrect legend order.
Plot 2. Correct legend order.
Please let me know if you'd like a simplified version of my R code.
The dataset I am using can be downloaded here: Sea Around Us
guides(fill = guide_legend(reverse = TRUE) should work!
This question already has an answer here:
How To Avoid Density Curve Getting Cut Off In Plot
(1 answer)
Closed 6 years ago.
newbie here. I have a script to create graphs that has a bit that goes something like this:
png(Test.png)
ht=hist(step[i],20)
curve(insert_function_here,add=TRUE)
I essentially want to plot a curve of a distribution over an histogram. My problem is that the axes limits are apparently set by the histogram instead of the curve, so that the curve sometimes gets out of the Y axis limits. I have played with par("usr"), to no avail. Is there any way to set the axis limits based on the maximum values of either the histogram or the curve (or, in the alternative, of the curve only)?? In case this changes anything, this needs to be done within a for loop where multiple such graphs are plotted and within a series of subplots (par("mfrow")).
Inspired by other answers, this is what i ended up doing:
curve(insert_function_here)
boundsc=par("usr")
ht=hist(A[,1],20,plot=FALSE)
par(usr=c(boundsc[1:2],0,max(boundsc[4],max(ht$counts))))
plot(ht,add=TRUE)
It fixes the bounds based on the highest of either the curve or the histogram.
You could determine the mx <- max(curve_vector, ht$counts) and set ylim=(0, mx), but I rather doubt the code looks like that since [] is not a proper parameter passing idiom and step is not an R plotting function, but rather a model selection function. So I am guessing this is code in Matlab or some other idiom. In R, try this:
set.seed(123)
png("Test.png")
ht=hist(rpois(20,1), plot=FALSE, breaks=0:10-0.1)
# better to offset to include discrete counts that would otherwise be at boundaries
plot(round(ht$breaks), dpois( round(ht$breaks), # plot a Poisson density
mean(ht$counts*round(ht$breaks[-length(ht$breaks)]))),
ylim=c(0, max(ht$density)+.1) , type="l")
plot(ht, freq=FALSE, add=TRUE) # plot the histogram
dev.off()
You could plot the curve first, then compute the histogram with plot=FALSE, and use the plot function on the histogram object with add=TRUE to add it to the plot.
Even better would be to calculate the the highest y-value of the curve (there may be shortcuts to do this depending on the nature of the curve) and the highest bar in the histogram and give this value to the ylim argument when plotting the histogram.