facet-wrap

Showing multiple axis labels using ggplot2 with facet_wrap in R

…衆ロ難τιáo~ 提交于 2019-11-30 18:56:46
I've got a nice facet_wrap density plot that I have created with ggplot2 . I would like for each panel to have x and y axis labels instead of only having the y axis labels along the left side and the x labels along the bottom. What I have right now looks like this: library(ggplot2) myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replace=T) myValues <- rnorm(300) df <- data.frame(myGroups, myValues) p <- ggplot(df) + geom_density(aes(myValues), fill = alpha("#335785", .6)) + facet_wrap(~ myGroups) p Which returns: (source: cerebralmastication.com ) It seems like this should be simple, but my

ggplot add ticks to each plot in a facet_wrap

╄→尐↘猪︶ㄣ 提交于 2019-11-30 04:02:13
问题 I'd like to display x-axis ticks on plots in the upper rows in facet_wraps. For example: library(ggplot2) ggplot(diamonds, aes(carat)) + facet_wrap(~ cut, scales = "fixed") + geom_density() generates this plot: I'd like to have ticks as I've drawn in on this plot: Is there a simple way to achieve this result? 回答1: Using scales = "free_x" adds x axes to each plot: ggplot(diamonds, aes(carat)) + geom_density() + facet_wrap(~cut, scales = "free_x") However, as you can see and the syntax suggests

Expression in ggplot2 facet labels

一曲冷凌霜 提交于 2019-11-30 03:38:22
问题 I would like to have an R expression in a ggplot2 facet label. Let's say I'm plotting the tips data.frame : library(reshape2) > head(tips) total_bill tip sex smoker day time size 1 16.99 1.01 Female No Sun Dinner 2 2 10.34 1.66 Male No Sun Dinner 3 3 21.01 3.50 Male No Sun Dinner 3 4 23.68 3.31 Male No Sun Dinner 2 5 24.59 3.61 Female No Sun Dinner 4 6 25.29 4.71 Male No Sun Dinner 4 as follows: library(ggplot2) sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1) +

Showing multiple axis labels using ggplot2 with facet_wrap in R

感情迁移 提交于 2019-11-30 02:54:31
问题 I've got a nice facet_wrap density plot that I have created with ggplot2 . I would like for each panel to have x and y axis labels instead of only having the y axis labels along the left side and the x labels along the bottom. What I have right now looks like this: library(ggplot2) myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replace=T) myValues <- rnorm(300) df <- data.frame(myGroups, myValues) p <- ggplot(df) + geom_density(aes(myValues), fill = alpha("#335785", .6)) + facet_wrap(~

Changing the Appearance of Facet Labels size

末鹿安然 提交于 2019-11-29 17:26:50
问题 I know the question was asked here: Is there a way to increase the height of the strip.text bar in a facet? I want to decrease the height of the strip.text bar without changing the text size. In the current case there is always a space left between text and strip bar walls. Here is what I tried so far, library(gcookbook) # For the data set library(ggplot2) ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") + facet_grid(.~ Date) + theme(strip.text = element_text(face=

ggplot2: Coloring axis text on a faceted plot

℡╲_俬逩灬. 提交于 2019-11-29 06:43:48
I seem unable to correctly color axis text on a faceted plot when the scales parameter is set to "free" . Consider the following dataset: library( ggplot2 ) X <- data.frame( V1 = LETTERS, V2 = runif( 26 ), V3 = rep( c("F1", "F2"), each = 13 ) ) We can plot the data on a single facet, highlighting the letters D, O, T as follows: v <- ifelse( X$V1 %in% c( "D", "O", "T" ), "red", "black" ) g <- ggplot( X, aes( x = V1, y = V2 ) ) + geom_point() + theme( axis.text.x = element_text( color = v ) ) Making the plot faceted using the default scales = "fixed" correctly highlights D, O, T on both facets.

ggplot2: Problem with x axis when adding regression line equation on each facet

旧时模样 提交于 2019-11-28 12:53:38
Based on the example here Adding Regression Line Equation and R2 on graph , I am struggling to include the regression line equation for my model in each facet. However, I don't figure why is changing the limits of my x axis. library(ggplot2) library(reshape2) df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6), M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1)) df <- melt(df, id = c("year")) ggplot(data = df, mapping = aes(x = year, y = value)) + geom_point() + scale_x_continuous() + stat_smooth_func(geom = 'text', method = 'lm', hjust = 0, parse = T) + geom_smooth(method = 'lm',

Flip facet label and x axis with ggplot2

。_饼干妹妹 提交于 2019-11-28 11:29:45
I am looking to flip the labels on a faceted panel of 1 row and 5 columns, so that the facet headers appear on bottom, and the x axis appears on top of facets. The reason is that I want to reuse those headers for a table that will be directly below the graph. So in this example... library(ggplot2) my.hist<-ggplot(diamonds, aes(clarity)) + geom_bar() my.hist + facet_wrap( ~ cut, ncol=5) + coord_flip() I would want the "cut" labels to show up below the chart. I was thinking that facet_grid might hold the key, but is only a guess. Anyone know how to accomplish this? Getting the facet strips below

ggplot2: Splitting facet/strip text into two lines

好久不见. 提交于 2019-11-28 09:54:10
Consider the following ggplot2 graph with long facet/strip text broken in two lines. The text goes outside the area devoted to facet titles. library(ggplot2) x <- c(1:3, 1:3) y <- c(3:1, 1:3) grp <- c(0, 0, 0, 1, 1, 1) p <- qplot(x=x, y=y) + geom_line() + facet_wrap(~ grp) grob <- ggplotGrob(p) strip.elem.y <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name grob <- geditGrob(grob, strip.elem.y[1], label="First line and\n second line" ) grid.draw(grob) Is there a way to increase the height of the strip text area ? I tried this a variety of ways but was frustrated getting the

Change the number of breaks using facet_grid in ggplot2

杀马特。学长 韩版系。学妹 提交于 2019-11-27 21:56:17
问题 I have a kind of data such as: y<-rep(c(1, 2, 3), times=5) group<-rep(c("a", "b", "c", "d", "e"), each=3) x<-c(2, 3, 4, 5, 7, 10, 10, 15, 19, 8, 10, 14, 25, 28, 33) a<-data.frame (x, y, group) and when I use facet_grid() with scales="free_x" option I obtain 5 graphs with different number of breaks. It is possible that the 5 graphs have the same number of breaks? For example 3. ggplot(a, aes(x, y))+geom_point()+ facet_grid(~group, scales="free_x") I know that if I remove the scales="free_x"