ggmosaic plot with asymmetric offset - r

I have a mosaic plot generated by ggmosaic:
ggplot(data.frame(a1=c(T,T,F,F), a2=c(T,F,T,F), a3=c(1,3,3,3))) +
geom_mosaic(aes(weight=a3, x=product(a1,a2), fill=a1))
I would like to widen the space between the vertical bars without changing the height of the space between the stacked columns:
I've tried using the offset parameter, but it seems to work on both dimensions, and can't isolate just one. An answer using vanilla ggplot is acceptable, but a ggmosaic-only solution is preferred.

A less-than-ideal workaround using using geom_bar:
ggplot(data.frame(a1=c(T,T,F,F), a2=c(T,F,T,F), a3=c(1,3,3,3)), aes(width=c(.4,.6,.4,.6)))+
geom_bar(aes(x=a2, y=-a3, fill=a1), position = "fill", stat = "identity")

Related

Change space between bars in histogram - R

I created a histogram in RStudio with the following code:
ggplot(data_csv, aes(x=Phasenew, fill=Success)) +
geom_histogram(binwidth = 1, position = "dodge", color="white")
What I want to do now, is to add more space between the bars of the histgram. I already tried the "width" parameter, but that one obviously does not work in histogram. Also I tried to make the outline bigger in white, but this will not show the correct length of the bar. Does anyone has an idea how to do that?
As two people wrote already in the comments, I also feel that your attempt to change the space between the 'bar' of a histogram is based on a misunderstanding about the nature of a histogram. Here the frequency of your events is represented as areas of the cells in the histogram. Or to quote Wikipedia:
the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval
A priori these cells do not even need to have the same width (in the case your class widths would differ).
Perhaps what you are looking for is geom_bar (https://ggplot2.tidyverse.org/reference/geom_bar.html)
ggplot(data_csv, aes(x=Phasenew, fill=Success)) +
geom_bar()

How do I use ggrepepl to avoid all geoms?

I'm trying to use ggrepel to create text labels for charts that I'm working on using R and ggplot2. I'm finding it very useful for repelling away from a single point, but I often run into a problem where it overlaps some other plot objects.
I'm trying to add it to the plot like so:
plot + ggrepel::geom_text_repel(aes(y = Ratio, label = Ratio), direction = "y")
Is there some way that I can tell ggrepel to avoid everything on the ggplot? I've tried searching and coming up with something for this but I'm stuck.
I hope my question is clear enough, thank you.
ggrepel does not allow avoiding all geoms.
In your case, as a workaround, you can use nudge_y = 0.1 in to shift all labels up.
Often, in such cases, you would want more space for the labels. This can be achieved with e.g. + scale_y_continous(expand = scales::expansion(mult = c(0.05, 0.2)))
ggrepel will not label, but repel from, points with a empty ("") label. So in general, as a workaround, you can try to generate data that would cover the other geoms and include that data with empty labels in your geom_text_repel call.

How to reposition the axis title in ggplot2?

I would like to reposition (e.g., move up by 10px) the x-axis label of this chart
ggplot() + geom_bar(aes(x=carb, y=..count..),data=mtcars)
I thought I could use vjust but as mentioned in this github issue, the only way to shift the x-label vertically is by using margin:
theme(axis.text.x = element_text(margin=margin(0,0,-10,0)))
However, margin produces something very different than a simple repositioning of the x-title, in that it resizes the chart too. I could I simply move the x-label instead?
You could do it this way
ggplot() + geom_bar(aes(x=carb, y=..count..),data=mtcars)+
theme(axis.title.x=element_text(margin=margin(-5,0,0,0)))
Just change the numbers until you get a satisfying result.

geom_bar not showing every values

I want to draw a bar plot, with ggplot and geom_bar, but it seems that the behavior of geom_bar is not consistent. I don't understand why.
My data is a time series of precipitations:
library(ggplot2)
library(data.table)
library(lubridate)
set.seed(42)
dt1 <- data.table(dateHeure=seq(ymd_hms("2014-06-04 13:30:00"),
ymd_hms("2014-10-20 08:30:00"), by='1 hour'),
rain=sample(c(rep(5,15), rep(10,15), rep(20,10),
rep(30, 5), 40, rep(0, 3262))))
Then i plot it, and not all the data appears... Why is some data missing?
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain),
stat="identity",
fill="blue") # doesn't work!
But if i add the variable color in aes, then the plot is correct!
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain, color="rain"),
stat="identity",
width=0.2) # work properly
So someone know why geom_bar doesn't work properly without color? Because i can't rely on it if sometimes not all the data is correctly plotted...
thanks!
edit: to respond to #eipi10, i added the plots. The strange thing is that when i resize the plot window in the first case, the data which is plotted changes!
Based on the edit to your question, I think I know what's happening: In the first plot, you use fill="blue". But the bin widths are very small compared to the overall range of the x-axis. This results in very, very thin vertical bars--so thin that you can't see some of them on your screen, but they appear when you expand the physical width of the plot.
On the other hand, in your second plot you used colour="rain", which adds a border to each bar, making each bar thicker, so they are visible, even when the physical width of the plot is relatively small.
Try adding colour="blue"(or "red" or whatever) to your first plot and I think you'll see all the bars, even without resizing. On the other hand, try changing colour="rain" to fill="rain" on your second plot and see if that creates the "disappearing data" effect on your second plot.
UPDATE: In response to your comment, you can use the colour parameter and then set the line width to get exactly the bar thickness you want, so you don't really need fill. For example:
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain),
stat="identity",
colour="blue", lwd=0.5)
Just set lwd (line width) to a value that gives you the bar-width you want. And, of course, you can also change the colour as well.

Adjusting the relative space of facets (without regard to coordinate space)

I have a primary graph and some secondary information that I want to facet in another graph below it. Facetting works great except I do not know how to control the relative space used by one facet versus another. Am aware of space='free' but this is only useful if the ranges correspond to the desired relative sizing.
So for instance, I may want a graph where the first facet occupies 80% and the second 20%. Here is an example:
data <- rbind(
data.frame(x=1:500, y=rnorm(500,sd=1), type='A'),
data.frame(x=1:500, y=rnorm(500,sd=5), type='B'))
ggplot() +
geom_line(aes(x=x, y=y, colour=type), data=data) +
facet_grid(type ~ ., scale='free_y')
The above creates 2 facets of equal vertical dimension. Adding in space='free' in the facet_grid function changes the dimensions such that the lower facet is roughly 5x larger than the upper (as expected).
Supposing I want the upper to be 2x as large, with the same data set and ordering of facets. How can I accomplish this?
Is the only way to do this with some trickery in rescaling the data set and manually overriding axis labels (and if so, how)?
Alternative
As indicated below can use viewports to render as multiple graphs. I had considered this and in-fact had implemented using this approach in the past with standard plot and viewports.
The problem is that it is very difficult to get x-axis to align with this approach. So if there is a way to fix the size of the y-axis label region and the size of the legend region, can produce 2 graphs that have the same rendering area.
You don't need to use facets for this - you can also do this by using the viewport function.
> ratio = 1/3
> v1 = viewport(width=1,height=ratio,y=1-ratio/2)
> v2 = viewport(width=1,height=1-ratio,y=(1-ratio)/2)
> print(qplot(1:10,11:20,geom="point"),vp=v1)
> print(qplot(1:10,11:20,geom="line"),vp=v2)
Ratio is the proportion of the top panel to the whole page. Try 2/3 and 4/5 as well.
This approach can get ugly if your legend or axis labels in the two plots are different sizes, but for a fix, see the align.plots function in the ggExtra package and ggplot2 author Hadley Wickam's notes on this very topic.
There's no easy way to do this with facets currently, although if you are prepared to go down to editing the Grid, you can modify the ggplot graph after it has been plotted to get this effect.
See also this question on using grid and ggplot2 to create join plots using R.
Kohske Takahashi posted a patch to facet_grid that allows specification of the relative sizing of facets. See the thread:
http://groups.google.com/group/ggplot2/browse_thread/thread/7c5454dcc04bc7b8
With luck we'll see this in a future version of ggplot2.

Resources