I used the facetting in ggplot. My question is: how is it possible to draw ticks between facets? I am aware of the scale = "free_y" option which gives ticks and values:
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_wrap( ~ cyl, scale = "free_y")
But only ticks?
This is basically what I want:
Related
I'm doing something like the following. I tend to use cowplot for saving images of ggplot-generated plots but non-cowplot solutions are also fine. The first plot produces three facets (and one empty space) in a 2x2 arrangement, the second produces 6 facets in a 3x2 arrangement. I set base_height and base_width assuming a size of 2 square for each plot. In the images generated from the code below, the individual plots (each facet) are not quite the same size, across the two images.
library(ggplot2)
library(cowplot)
ggplot(mtcars, aes(x=mpg, y=hp))+
geom_point()+
facet_wrap(~cyl,ncol=2) +
ggtitle("hp vs. mpg, by cyl") +
theme_cowplot(font_size=10)
save_plot("car1.png", last_plot(), base_height=4, base_width=4)
ggplot(mtcars, aes(x=mpg, y=hp))+
geom_point()+
ggtitle("hp vs. mpg, by carb") +
facet_wrap(~carb,ncol=3)+
theme_cowplot(font_size=10)
save_plot("car2.png", last_plot(), base_height=4, base_width=6)
I tried including the png files in the post but they each get scaled differently so it would be misleading.
I know I could generate each facet separately as its own plot and use plot_grid, and then base_height and base_width would set the size of each plot on its own, but is there any way to use facet_wrap or facet_grid and set the absolute size when saved of each facet?
Does cowplot::align_plots work for you?
library(ggplot2)
library(cowplot)
p1 <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
facet_wrap(~cyl, ncol = 2) +
ggtitle("hp vs. mpg, by cyl") +
theme_cowplot(font_size = 10)
p2 <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
ggtitle("hp vs. mpg, by carb") +
facet_wrap(~carb, ncol = 3) +
theme_cowplot(font_size = 10)
twoplots <- align_plots(p1, p2, align = "hv", axis = "tblr") #tblr' for aligning all margins
save_plot("car1.png", ggdraw(twoplots[[1]]))
save_plot("car2.png", ggdraw(twoplots[[2]]))
I need to plot the geom_histogram but as the plots overlap, I want to plot it linear instead of bars, i.e, the x=hp, y=count (percent).
Can anybody please help me find how to do it.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp)) +
geom_histogram(binwidth=5)
I have done it by qplot but I need to do it in ggplot and the percent.
qplot(hp, data = mtcars, geom = "freqpoly", binwidth = 10)
Thanks
You can use geom_freqpoly:
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp, y=..count../sum(..count..))) +
geom_freqpoly(binwidth=5) +
scale_y_continuous(labels = percent_format()) +
ylab("Percent")
I have three different datasets that representing data for different year, they all have same y-axis and x-axis? For example like the plot shown below except this is in R and it is dotplot
One approach to combining graphs for display is create them separately with ggplot and combine them with patchwork.
library(ggplot2)
install.packages("devtools")
devtools::install_github("thomasp85/patchwork")
library(patchwork)
p1 <- ggplot(mtcars, aes(x = mpg)) + geom_dotplot()
p2 <- ggplot(mtcars, aes(x = hp)) + geom_dotplot()
p3 <- ggplot(mtcars, aes(x = wt)) + geom_dotplot()
p1 + p2 + p3
#or
ggplot(mtcars) +
geom_dotplot(aes(mpg)) +
ggplot(mtcars) +
geom_dotplot(aes(hp)) +
ggplot(mtcars) +
geom_dotplot(aes(wt))
Also cowplot::plot_grid https://cran.r-project.org/web/packages/cowplot/vignettes/plot_grid.html
In a faceted plot from ggplot2 package it is possible to change the position of tick marks by the functions like scale_x_continuous(position="top"). However, if tick marks are to be placed on top, they appear above faceting panels, as here:
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() +
facet_grid(. ~ cyl) +coord_flip() +
scale_y_continuous(position="right")
Is it possible to place individual tick marks scales under the denotations of facets?
How about pushing the facet panel to the bottom?
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() +
facet_grid(. ~ cyl, switch='x') +coord_flip() +
scale_y_continuous(position="right")
I am trying to create a plot with facets. Each facet should have its own scale, but for ease of visualization I would like each facet to show a fixed y point. Is this possible with ggplot?
This is an example using the mtcars dataset. I plot the weight (wg) as a function of the number of miles per gallon (mpg). The facets represent the number of cylinders of each car. As you can see, I would like the y scales to vary across facets, but still have a reference point (3, in the example) at the same height across facets. Any suggestions?
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
geom_hline (yintercept=3, colour="red", lty=6, lwd=1) +
facet_wrap( ~ cyl, scales = "free_y")
[EDIT: in my actual data, the fixed reference point should be at y = 0. I used y = 3 in the example above because 0 didn't make sense for the range of the data points in the example]
It's unclear where the line should be, let's assume in the middle; you could compute limits outside ggplot, and add a dummy layer to set the scales,
library(ggplot2)
library(plyr)
# data frame where 3 is the middle
# 3 = (min + max) /2
dummy <- ddply(mtcars, "cyl", summarise,
min = 6 - max(wt),
max = 6 - min(wt))
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
geom_blank(data=dummy, aes(y=min, x=Inf)) +
geom_blank(data=dummy, aes(y=max, x=Inf)) +
geom_hline (yintercept=3, colour="red", lty=6, lwd=1) +
facet_wrap( ~ cyl, scales = "free_y")