How to enlarge the size of facet in R - r

I'm working on the following dataset where each facet shows the bleaching for one kind of coral at one site across the time period. My problem is how to enlarge the size of each facet to see the trend more clearly, as in current facets, it is hard to see the trend because of the small change in bleaching....
here is my code,
cb1<-aggregate(cb$latitude, list(Site=cb$site), mean)
cb$site=factor(cb$site, levels=cb1$Site[order(cb1$x)])
ggplot(cb,aes(year,bleaching)) +
geom_point() +
facet_grid(site~kind) +
geom_smooth(method="lm",color="grey") +
coord_cartesian(ylim=c(0,1))
due to the current size of the grid of facets, some lines seem flat but actually they are not.

You cannot really increase the sizes of the facets unless you increases the size of the plot overall. One option would be to save a large version of the plot:
p<-ggplot(cb,aes(year,bleaching))+geom_point()+facet_grid(site~kind)+geom_smooth(method="lm",color="grey")+coord_cartesian(ylim=c(0,1))
ggsave("file_name.jpg", plot = p, width = 24, height = 24, units = "in")
If you have limited space (e.g. the plot has to go on an A4 sheet) then the facet_grid_paginate function from ggforce would be a good option. It allows you to split faceted plots over multiple pages. You can define the number of rows and columns per page. See this link.
Alternatively, if you want to show that the lines are not flat more clearly, you can try toying with a couple of the arguments to facet_grid. facet_grid allows you to set the scales to free, free_x or free_y. Setting free_y would mean that each facet has its own y-axis (not necessarily between 0 and one (assuming you also removed the ylim=c(0,1). This would, however, make the the facets more difficult to compare with each other.

Related

Formatting legend to fit 45+ legend items in R

I need help formatting a legend in ggplot2. I have approximatley 45 legened items. When I display the legend, my graph shrinks becuase the graph and legend items don't fit. I'm wondering how I can get all my legend items to display, but also have a reasonably sized graph. Is there a way to make my longer legend items go over multiple lines? Or, is there a way to make some legend items occupy more of the white space above/below the page? Any help will be super appreciated! Below is a screenshot of my current plot, along with my code.
guild_chart <-
ggplot(chart, aes(x=factor(Site,level=level_order1), y=`Row 1`, fill=Label)) +
geom_bar(stat="identity") +
scale_fill_manual(values =colfundose) +
theme_bw()+ ylab("# of reads") +
xlab("Location")
A frame challenge, if I may:
This is probably a bad way to visualise this data. The groups are impossible to distinguish from one another, and very difficult to compare. What is the purpose of this graph? What information do you wish to convey to the viewer? With that question in mind, think about how you can design the graph in a legible way.
To increase legibility, I would consider combining factors into groups and visualising these instead of the individual levels you are currently displaying.
As others have noted, presenting your data in this stacked bar layout is difficult to interpret. In addition to the challenge in discriminating between different groups, its also tough to estimate the number of reads for any sort of comparison.
As an alternative presentation, would it make sense to visualize these read counts as a heatmap? You could have a column (or row) for each of your seven locations containing 45 squares, colored to indicate # of reads. Now your legend is a color gradient with the range of read counts across the dataset. An advantage here is you can keep your 45 categories, if this is important, but have them right next to their respective rows, minimizing lookups in a legend.

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()

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.

How can I specify the exact number of pixels I would like my ggplot to take up?

How do I adjust the overall size of a ggplot?
I'm using Shiny, thus, I'd like to control the size of my plot. I'd like one of my plots to be the size of a postage stamp. And, I'd like the plot next to it to be huge.
From what I can tell, no matter how much I play with scale_x_continuous or xlim or cartesian_coord or whatnot, I'm still stuck with a ggplot that has made up it's own mind on how big it wants to be. I can squish down the amount of ink within the size of the plot, or I can fill up the insides of the plot by using various attributes, but I can't change the number of pixels the plot takes up on my screen.
How can I specify the exact number of pixels I would like my plot to be?
I don't know shiny, but maybe this helps,
library(grid)
print(qplot(1,1), vp=viewport(width=unit(114,"points"), height=unit(1.4,"inch")))

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