facet-wrap

Is it possible to reorder only the facets of facet_wrap, without reordering the underlying factor levels?

早过忘川 提交于 2019-12-12 10:38:48
问题 Sample data frame: df <- data.frame(x=rep(1:10,4),y=rnorm(40),Case=rep(c("B","L","BC","R"),each=10)) I can plot each time series in its own facet with: ggplot(df,aes(x=x,y=y,color=Case)) + geom_line()+facet_wrap(~Case,nr=2,nc=2) Now, suppose I want to change the facet order to (starting from top-left and going to bottom-right along rows) "L","B","R","BC". The usual suggestion here on SO is to do this. However, if I reorder the levels of factor Case , also the colors of the curves will be

Annotate x-axis with N in faceted plot, but preserve empty facets

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:44:13
问题 I asked a question yesterday about annotating the x-axis with N in a faceted plot using a minimal example that turns out to be too simple, relative to my real problem. The answer given there works in the case of complete data, but if you have missing facets you would like to preserve, the combination of facet_wrap options drop=FALSE and scales="free_x" triggers an error: "Error in if (zero_range(from) || zero_range(to)) { : missing value where TRUE/FALSE needed" Here is a new, less-minimal

R, Incorrect output from ggplot2 and facet_wrap when using spatial data

烂漫一生 提交于 2019-12-11 22:03:10
问题 I'm trying to create a visualisation of the UK for different time periods where the fill for each region depends on a numeric variable. When I split the dataframe by time period and create the charts independently in ggplot2, they are correct. However, when I use the facet_wrap function to have them appear in a single chart, the outputs are incorrect (the numbers for each region are not correct). (Note, I have tried to follow this tutorial for spatial data in R as closely as possible: http:/

Obtaining different results when using `facet_wrap()` in `ggplot`

一笑奈何 提交于 2019-12-11 15:15:35
问题 I am using this code to download data from yahoo finance and plot some stocks against the S&P500 after normalising the adjusted prices. The following code returns; Ra <- c("NFLX") %>% tq_get(get = "stock.prices", from = "2013-01-01", to = "2016-12-31") Rb <- "SPY" %>% tq_get(get = "stock.prices", from = "2013-01-01", to = "2016-12-31") stock_returns_daily <- Ra benchmark_returns_daily <- Rb RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date")) normalise

facet_wrap add geom_hline

好久不见. 提交于 2019-12-11 15:14:24
问题 I have the following code for my ggplot - the facet_wrap function draws out 20 plots on the page for each Name and there are 5 Pcode along the x-axis. I would like to calculate the average TE.Contr for each Name and plot that value as a horizontal line on each of the plots (which are split out by Facet_wrap). Currently my codes plots the average of ALL TE.Contr. values instead of the average TE.Contr. of the specific Name. T<-ggplot(data = UKWinners, aes(x = Pcode, y = TE.Contr., color =

How do I plot male and female data separately?

为君一笑 提交于 2019-12-11 14:08:17
问题 I'm trying to plot Female and Male data for each year in a facet wrap plot. As an example, for the year 2013 there are 10,949 data points for female and 53,351 data points for male. Here's a sample of the data: cost gender year 1 305.665 Female 2013 2 194.380 Female 2013 3 462.490 Female 2013 4 200.430 Female 2013 5 188.570 Female 2013 6 277.245 Female 2013 The code I put together is: library(ggplot2) costs<-read.table("cost_data.txt",header=TRUE) df<-data.frame(costs) ggplot(df, aes(df$cost

ggplot2: how to use facet_wrap_labeller to get correct subscripts

烂漫一生 提交于 2019-12-11 12:34:39
问题 This question refers to, and requires (but maybe not?), the function facet_wrap_labeller written by Roland here: https://stackoverflow.com/a/16964861/3275826 MWE for my case: dput(test) structure(list(V1 = c(0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 3.67, 3.73, 3.79, 3.85, 3.91, 3.97), V2 = c(0.0291598, 3.40333, 1.3881, 0.15733, 0.0200618, 0.00145373, 0.332262, 0.30233, 0.288497, 0.267876, 0.264134, 0.227544), V3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 19L, 19L, 19L, 19L, 19L, 19L), .Label = c("Param0",

ggplot2: create a plot using selected facets with part data

我怕爱的太早我们不能终老 提交于 2019-12-11 11:50:03
问题 I would like to create a plot with Using part of the data to create a base plot with facet_grid of two columns. Use remaining part of the data and plot on top of the existing facets but using only a single column. The sample code: library(ggplot2) library(gridExtra) df2 <- data.frame(Class=rep(c('A','B','C'),each=20), Type=rep(rep(c('T1','T2'),each=10), 3), X=rep(rep(1:10,each=2), 3), Y=c(rep(seq(3,-3, length.out = 10),2), rep(seq(1,-4, length.out = 10),2), rep(seq(-2,-8, length.out = 10),2))

overlapping y-scales in facet (scale=“free”)

坚强是说给别人听的谎言 提交于 2019-12-11 10:11:13
问题 I've been learning ggplot in the last few weeks. Generally, I'm getting things done (slowly though), but now I'm stuck. I created the following facetted plot: http://dl.dropbox.com/u/7752237/example_bad_y_scales.pdf Faceting is done by pl <- pl + facet_wrap(~sci_name,ncol=1,scale="free") The Problem: Numbers on the y-scale don't look good, especially the scales that go from 0-70 (numbers overlapping). I'd like the somehow change the number of breaks on the y-scale (to let's say just 1 or 2

R ggplot2 facet_wrap isn't changing data according to Factor level

99封情书 提交于 2019-12-11 05:26:07
问题 I am trying to use facet_wrap to break up my data frame into individual plots based on one column. However, when I use levels, the title above each individual plot changes, but the data displayed in the chart does not. Here's an example: library(reshape2) library(ggplot2) levels(tips$sex) <- c("Male", "Female") ggplot(tips, aes(x=total_bill, y=tip/total_bill))+ facet_wrap(~sex)+ geom_point(shape=1) ggsave("prac.pdf") This gives me a plot with Male on the left and Female on the right. The