How to plot several plots with same legend ggplot2, R - r

I just came into a problem while making several maps in R, the problem I came to is that I want to plot several maps and some geom_points in those maps, each map will have some points with different values and so the legend with the scales (size and color) will change between maps. All I want is to have exactly the same legend, representing the same values (for both color and size). I've tried with breaks etc but my data is continuous, so I didn't find any way to fix it.
EDIT:Simple example
Will try to explain with simple example by myself. Imagine I have these two arrays to be plotted into different coordinates for 2 different days:
c<-(1,2,3,2,1)
c<-(1,9,2,1,2)
What I want is to set the legend of the plot to be always representing the range 1-9 as values of the geom_points, no matter the specific values of the given day, in a way that no matter the values, the legend will be always the same and if I try to set some slides, the scale will not change
Any ideas?

Related

r - how to set plot size regardless of legend width ggplot2

I'm looping through a spatial feature dataframe and creating hundreds of maps from each column using ggplot2. Depending on the data present in the column, the legend for each map may be a different size, making the plot size different for each map. Although subtle, you can see below that the first map is offset to the left relative to the second map because of the longer label in the legend
I'd like to maintain the size of the plot area (in this case, the map area) so that all the saved maps are consistently sized and lined up when flipping through them. I've looked through questions and answers on here, but all I've been able to find are solutions for how to preserve aspect/ratio of different plots on the same image/document as described here. I'd like to be able to save each map as its own file.
Can anyone give me suggestions about how to keep the plot size consistent regardless of legend width when saving these plots as images using ggplot2?

Is it possible to edit the numbers displayed on an axis without moving the points that were plotted?

I've come up with a graph (a scatterplot) of the log(1+inf) (inf = number of people infected with a given disease on the y-axis against one of the explanatory variables, in this case, the populational density (pop./kmĀ²; x-axis) on my model. The log transformation was used merely for visualization, because it spreads the distribution of the data and allows for more aesthetically appealing plots. Basically, what I want is both axis to show the value of that same variable before the log transformation. The dots need to be plotted like plot(log(1+inf),log(populational_density), but the number on the axis should refer to plot(inf,populational_density). I've provided a picture of my graph with some manual editing on the y-axis to show you the idea of what I want.
The numbers in red would be the 'inf' values equivalent to log(inf);
Please, bear in mind that those values in red do not correspond to reality.
I understand the whole concept of y = f(x), but i've been asked to provide it. Is this possible? I'm using the ggplot2package for plotting.

Mixed geom_line & geom_point plot: remove marker from color scale

I often have to use plots mixing lines and points (ggplot2), with the colors of the line representing one variable (here, "Dose"), and the shape of the points another one (here, "Treatment). Figure 1 shows what I typically get:
Figure 1: what I get
I like having different legends for the two variables, but would like to remove the round markers from the color scale, to only show the colors (see legend mockup below, made with Gimp). Doing so would allow me to have a clean legend, with colors and shapes clearly segregated.
Figure 2 (mockup): what I would like
Would anyone know if there is a way to do that? Any help would be much appreciated.
Note: the plots above show means and error bars, but I have the same problem with any plot mixing geom_line and geom_point, even simple ones.
Thanks in advance !

ggplot draw multiple plots by levels of a variable

I have a sample dataset
d=data.frame(n=rep(c(1,1,1,1,1,1,2,2,2,3),2),group=rep(c("A","B"),each=20),stringsAsFactors = F)
And I want to draw two separate histograms based on group variable.
I tried this method suggested by #jenesaisquoi in a separate post here
Generating Multiple Plots in ggplot by Factor
ggplot(data=d)+geom_histogram(aes(x=n,y=..count../sum(..count..)),binwidth = 1)+facet_wrap(~group)
It did the trick but if you look closely, the proportions are wrong. It didn't calculate the proportion for each group but rather a grand proportion. I want the proportion to be 0.6 for number 1 for each group, not 0.3.
Then I tried dplyr package, and it didn't even create two graphs. It ignored the group_by command. Except the proportion is right this time.
d%>%group_by(group)%>%ggplot(data=.)+geom_histogram(aes(x=n,y=..count../sum(..count..)),binwidth = 1)
Finally I tried factoring with color
ggplot(data=d)+geom_histogram(aes(x=n,y=..count../sum(..count..),color=group),binwidth = 1)
But the result is far from ideal. I was going to accept one output but with the bins side by side, not on top of each other.
In conclusion, I want to draw two separate histograms with correct proportions calculated within each group. If there is no easy way to do this, I can live with one graph but having the bins side by side, and with correct proportions for each group. In this example, number 1 should have 0.6 as its proportion.
By changing ..count../sum(..count..) to ..density.., it gives you the desired proportion
ggplot(data=d)+geom_histogram(aes(x=n,y=..density..),binwidth = 1)+facet_wrap(~group)
You actually have the separation of charts by variable correct! Especially with ggplot, you sometimes need to consider the scales of the graph separately from the shape. Facet_wrap applies a new layer to your data, regardless of scale. It will behave the same, no matter what your axes are. You could also try adding scale_y_log10() as a layer, and you'll notice that the overall shape and style of your graph is the same, you've just changed the axes.
What you actually need is a fix to your scales. Understandable - frequency plots can be confusing. ..count../sum(..count..)) treats each bin as an independent unit, regardless of its value. See a good explanation of this here: Show % instead of counts in charts of categorical variables
What you want is ..density.., which is basically the count divided by the total count. The difference is subtle in principle, but the important bit is that the value on the x-axis matters. For an extreme case of this, see here: Normalizing y-axis in histograms in R ggplot to proportion, where tiny x-axis values produced huge densities.
Your original code will still work, just substituting the aesthetics I described above.
ggplot(data=d)+geom_histogram(aes(x=n,y=..density..,)binwidth = 1)+facet_wrap(~group)
If you're still confused about density, so are lots of people. Hadley Wickham wrote a long piece about it, you can find that here: http://vita.had.co.nz/papers/density-estimation.pdf

Showing two different scales on the axis

I have this sample code where I am plotting two variables on two vertical axis.
One has real range 0-1 and another one has integer range 0-4. I would like to show
both values simultaneously, but I do not know how.
I looked into this example on cross validated that was accepted, but I noticed that when I run the code, both vertical axes have the same scale.
I would need for two scales to be different. Please help.

Resources