[R] ggplot with geom_tile
Dear R-users, I am trying to make a plot with ggplot-geom_tile(), but cannot remove some unwanted (white) lines through my plot. Below a reproducible example: # library(ggplot2) tot=as.data.frame(rep(seq(-50,50,5),each=21)) names(tot)=precip temp=rep(seq(-5,5,0.5),21) tot$temp=temp disc=array(dim=c(21,21)) for(i in 1:21){ for(y in 1:21){ temp- rev(seq(1+((i-1)*500),12000+((i-1)*500),100)) disc[i,y]=temp[y] }} disc1=t(disc) bla=as.vector(disc1) tot$dis=bla cols=c(darkred, white, darkblue) p - ggplot(tot, aes(x=temp, y=precip,group=dis)) p+geom_tile(aes(fill=dis)) + scale_fill_gradientn(colours=cols) ### This creates the plot that I want, but with white horizontal and vertical lines that I do not need. I have tried different things, but have not found the solution. Can you help me with this problem? Kind regards, Saskia van Pelt __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] ggplot with geom_tile
Dear Paul and others, It also happens with the examples from Hadley geom_tile() on: http://had.co.nz/ggplot2/geom_tile.html http://had.co.nz/ggplot2/geom_tile.html With this example I do not get the white lines: p - ggplot(data = tot, mapping = aes(x = temp, y = precip)) p + layer(data = tot, mapping = aes(colour = dis, fill = dis), geom = tile, stat = identity) But in this example I don't have the colours going from red (1) to white, to blue (22000). My SessionInfo() sessionInfo() R version 2.13.1 (2011-07-08) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=Dutch_Netherlands.1252 LC_CTYPE=Dutch_Netherlands.1252 LC_MONETARY=Dutch_Netherlands.1252 [4] LC_NUMERIC=C LC_TIME=Dutch_Netherlands.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 loaded via a namespace (and not attached): [1] digest_0.5.1 tools_2.13.1 Thank you in advance! Kind regards, Saskia From: Hiemstra, Paul (KNMI) Sent: Fri 09/12/2011 12:23 To: Pelt van, Saskia (KNMI) Cc: r-help@r-project.org Subject: Re: [R] ggplot with geom_tile Dear Saskia, Thank you for the reproducible example, that helps a lot. I pasted you code into R and got a perfectly fine image. You need to provide us with your sessionInfo(). Mine is listed at the end of the mail. I seem to remember having this problem with R 2.12. Therefore updating R might be a good option. hope this helps, Paul sessionInfo() R version 2.13.0 (2011-04-13) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C [3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8 [5] LC_MONETARY=C LC_MESSAGES=en_US.utf8 [7] LC_PAPER=en_US.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C attached base packages: [1] grid stats graphics grDevices utils datasets methods [8] base other attached packages: [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 fortunes_1.4-2 loaded via a namespace (and not attached): [1] digest_0.5.1 On 12/09/2011 10:33 AM, Pelt van, Saskia (KNMI) wrote: Dear R-users, I am trying to make a plot with ggplot-geom_tile(), but cannot remove some unwanted (white) lines through my plot. Below a reproducible example: # library(ggplot2) tot=as.data.frame(rep(seq(-50,50,5),each=21)) names(tot)=precip temp=rep(seq(-5,5,0.5),21) tot$temp=temp disc=array(dim=c(21,21)) for(i in 1:21){ for(y in 1:21){ temp- rev(seq(1+((i-1)*500),12000+((i-1)*500),100)) disc[i,y]=temp[y] }} disc1=t(disc) bla=as.vector(disc1) tot$dis=bla cols=c(darkred, white, darkblue) p - ggplot(tot, aes(x=temp, y=precip,group=dis)) p+geom_tile(aes(fill=dis)) + scale_fill_gradientn(colours=cols) ### This creates the plot that I want, but with white horizontal and vertical lines that I do not need. I have tried different things, but have not found the solution. Can you help me with this problem? Kind regards, Saskia van Pelt __ R
Re: [R] ggplot with geom_tile
Dear Frederic, No this was not the problem, the problem are horizontal and vertical white lines in the figure. But the problem is gone when I save the figure and reopen it (in another program), so I think the problem is only with my direct plot outputs. Thanks for your help. Kind regards, Saskia From: Frederic Andrieu [mailto:fandr...@mango-solutions.com] Sent: Fri 09/12/2011 12:43 To: Pelt van, Saskia (KNMI) Cc: r-help@r-project.org; Hiemstra, Paul (KNMI) Subject: RE: [R] ggplot with geom_tile Dear Saskia, If you mean the grid lines around the edge then they can be removed by using the following: p - ggplot(tot, aes(x=temp, y=precip,group=dis)) p+geom_tile(aes(fill=dis)) + scale_fill_gradientn(colours=cols) + scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) I hope this helps I remain at your disposal should you have any questions. Best regards, Frederic Andrieu M: +44(0)07813 526 123 T: +44 (0)1249 766 801 F: +44 (0)1249 767 707 www.mango-solutions.com Unit 2 Greenways Business Park Bellinger Close Chippenham Wilts SN15 1BN UK -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Paul Hiemstra Sent: 09 December 2011 11:23 To: Pelt van, Saskia (KNMI) Cc: r-help@r-project.org Subject: Re: [R] ggplot with geom_tile Dear Saskia, Thank you for the reproducible example, that helps a lot. I pasted you code into R and got a perfectly fine image. You need to provide us with your sessionInfo(). Mine is listed at the end of the mail. I seem to remember having this problem with R 2.12. Therefore updating R might be a good option. hope this helps, Paul sessionInfo() R version 2.13.0 (2011-04-13) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C [3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8 [5] LC_MONETARY=C LC_MESSAGES=en_US.utf8 [7] LC_PAPER=en_US.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C attached base packages: [1] grid stats graphics grDevices utils datasets methods [8] base other attached packages: [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 fortunes_1.4-2 loaded via a namespace (and not attached): [1] digest_0.5.1 On 12/09/2011 10:33 AM, Pelt van, Saskia (KNMI) wrote: Dear R-users, I am trying to make a plot with ggplot-geom_tile(), but cannot remove some unwanted (white) lines through my plot. Below a reproducible example: # library(ggplot2) tot=as.data.frame(rep(seq(-50,50,5),each=21)) names(tot)=precip temp=rep(seq(-5,5,0.5),21) tot$temp=temp disc=array(dim=c(21,21)) for(i in 1:21){ for(y in 1:21){ temp- rev(seq(1+((i-1)*500),12000+((i-1)*500),100)) disc[i,y]=temp[y] }} disc1=t(disc) bla=as.vector(disc1) tot$dis=bla cols=c(darkred, white, darkblue) p - ggplot(tot, aes(x=temp, y=precip,group=dis)) p+geom_tile(aes(fill=dis)) + scale_fill_gradientn(colours=cols) ### This creates the plot that I want, but with white horizontal and vertical lines that I do not need. I have tried different things, but have not found the solution. Can you help me
[R] making dataframes
Dear all, I have a dataframe which looks like this (dummy): date-c(jan, feb, mar, apr, may, june, july, aug,sep,oct,nov,dec) col1-c(8.2,5.4,4.3,4.1,3.1,2.5,1.1,4.5,3.2,1.9,7.8,6.5) col2-c(3.1,2.3,4.7,6.9,7.5,1.1,3.6,8.5,7.5,2.5,4.1,2.3) dum-data.frame(cbind(date,col1,col2)) dum date col1 col2 1 jan 8.2 3.1 2 feb 5.4 2.3 3 mar 4.3 4.7 4 apr 4.1 6.9 5 may 3.1 7.5 6 june 2.5 1.1 7 july 1.1 3.6 8 aug 4.5 8.5 9 sep 3.2 7.5 10 oct 1.9 2.5 11 nov 7.8 4.1 12 dec 6.5 2.3 I would like to convert this data.frame into something that looks like this: date rainfall category 1 jan 8.2 col1 2 feb 5.4 col1 3 mar 4.3 col1 4 apr 4.1 col1 5 may 3.1 col1 6 june 2.5 col1 7 july 1.1 col1 8 aug 4.5 col1 9 sep 3.2 col1 10 oct 1.9 col1 11 nov 7.8 col1 12 dec 6.5 col1 1 jan 3.1 col2 2 feb 2.3 col2 3 mar 4.7 col2 4 apr 6.9 col2 5 may 7.5 col2 6 june 1.1 col2 7 july3.6 col2 8 aug 8.5 col2 9 sep 7.5 col2 10 oct 2.5 col2 11 nov 4.1 col2 12 dec 2.3 col2 So the column-names become categories. The dataset is rather large with many columns and a lengthy date-string. Is there an easy way to do this? Thank you for your help, Kind regards, Saskia van Pelt __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] : plot different axis, same plot position
Dear R-users, I am trying to make a plot in R where x and y are plotted in a regular way, but the x axis corresponds to another set of values. For example I have x,y and T (all 29 values) x- c( -1.31846232, -1.04744756, -0.87034853, -0.72883370, -0.60618971, -0.49501845, -0.39128988, -0.29250120, -0.19694055, -0.10334039, -0.01069355, 0.08185470, 0.17507665, 0.26971270, 0.36651292, 0.46627625, 0.56989300, 0.67839644, 0.79303127, 0.91535108, 1.04736522, 1.19177282, 1.35235778, 1.53470330, 1.74760041, 2.00616370, 2.33996397, 2.82073311, 3.72564504) y-c(51.85177, 53.67026, 60.64062, 62.33320, 62.81224, 63.20116,76.10719, 78.07620, 78.83859, 80.06188, 84.53568, 85.15358, 87.39279, 87.49965, 89.88347, 90.73792, 90.92971, 92.17759, 92.84064, 93.17964, 97.51360, 97.64690, 98.20756, 101.64150,104.91425, 112.88917, 116.90400, 121.50099, 126.43808) T- c(1.190283, 1.240506, 1.295154, 1.354839, 1.420290, 1.492386, 1.572193, 1.661017, 1.760479, 1.872611, 2.00, 2.145985, 2.314961, 2.512821, 2.747664, 3.030928, 3.379310, 3.818182, 4.388060, 5.157895, 6.255319, 7.945946, 10.89, 17.294118, 42.00) plot(x,y,xlab=x, ylab=10 day maximum (mm),col=blue,type=b,pch=1,lty=2) This plot is the correct plot, but I want the x-axis to display the values of T If I do this: plot(x,y,xlab=x, ylab=10 day maximum (mm),xaxt=n, col=blue,type=b,pch=1,lty=2) axis(1,las=0,T) than the T is plotted in stead of x, but on the same scale. So the tickmarks start at 1 and stop at 3. I would like each point of the graph to correspond to a value of T on the x-axis, but the plot position should still correspond to x. This means that y corresponds to the x values in plot position, but to the T value on the x-axis. I want this because the x values have no explanatory meaning (Gumbel variates), while the T values (return period) have, so I can use it to communicate what is happening in this graph. I hope somebody can help me with this. Kind regards, Saskia van Pelt __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] spatial plots maps-ssplot
Hi all, I have made a plot with ssplot, using a SpatialPointsDataFrame. The content is quite simple, as I have 9 grid points with lon/lat coordinates and 9 values attached to these coordinates. They are in a square area of 3 by 3 gridboxes. I would like to lay a map from maps() over these values, but when I try this, the grids of the maps (smaller) do not overlap with the grids I have already created with spplot(). I would like shaded gridboxes, the boxes should be filled with colour. Can anyone help me with this problem? Thank you in advance! This is part of my code: minlon-4.5 maxlon-12 minlat-46.5 maxlat-52.5 gt=GridTopology(cellcentre.offset=c(5.75,47.5),cellsize=c(2.5,2),cells.dim=c(3,3)) grd=SpatialGrid(gt,proj4string = CRS(as.character(NA))) gridparameters(grd) gd-as.data.frame(grd,SpatialGrid) h-season_djf h-as.data.frame(h) djf.att=SpatialPointsDataFrame(gd,h) gridded(djf.att)=TRUE spplot(djf.att, col.regions = colorRampPalette(c(red,orange, yellow, lightblue, blue, purple)), xlab=Longitude(°), ylab=Latitude(°),add=T,scales=list(draw=T),main=Seasonal change DJF MIUB, font.main=4) map('rivers', xlim=c(minlon,maxlon),ylim= c(minlat,maxlat), col=blue,add=T) map('worldHires', xlim = c(minlon, maxlon), ylim = c(minlat, maxlat), add = T, col = darkgrey,wrap=T) Regards, Saskia van Pelt __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] - combining lists
Hi all, I have used this library to create a (360 day)calendar for my rainfall data (which is divided over 9 gridcells): ## CODE## library(udunits) utInit() calendar - att.get.nc(nc,'time','calendar') T - var.get.nc(nc,time) times.list - utCalendar(T,days since 1961-01-01,style='array',calendar=calendar) ## END CODE## To separate months and years I use: ## CODE## times.ind - lapply(as.list(1:12), function(x,months) which(months == x), months = times.list$month) times.ind2 - lapply(as.list(1961:1990), function(x,years) which(years == x), years = times.list$year) ## END CODE## Now I have two lists, the first contains months, for example, for times.ind[1] I get a list of day numbers which are from January. (so 1:30, 361:390 etc) The other list does the same for years times.ind2[1] gives the days 1:360 It is now possible for me to create for example monthly averages for each grid cell ##CODE## pr.monthmean - sapply(times.ind, function(x,arr) rowMeans(arr[,,x],dims=2), arr = pr) ## END CODE## the same can be done for yearly averages. However, I would like to create a matrix with monthly averages for each year separately. I think I need to couple times.ind and times.ind2 in some way, but I do not know how to do this. Is there anyone who can help me with this? Thank you in advance, Kind regards, Saskia __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] 360-day calendar-netcdf
Hi, Recently I started working with netcdf IPCC climate model data and I am using R to analyze these data. Some problems occur while doing this, as I am using the output of different climate models which are different in for example time calendar and grid coordinates. To analyze monthly means I have been using the function /chron/ for 365 day calendar time series. Originally the time is in numbers (1,2,14600). This is for the timeperiod: 1961-2000. library(chron) syntime - var.get.nc(nc, time) time =chron(syntime, origin=c(month=1, day=1, year=1961)) jan - months(time)==Jan feb - months(time)==Feb mar - months(time)==Mar apr - months(time)==Apr may - months(time)==May jun - months(time)==Jun jul - months(time)==Jul aug - months(time)==Aug sep - months(time)==Sep oct - months(time)==Oct nov - months(time)==Nov dec - months(time)==Dec Now I can use for example mean(pr[jan]) to get the monthly average of precipitation (pr) for January. However, I cannot find how I can change the 365 day in a 360 day calendar for the 360-day data that I have (the 360-day calendar has 10799 days for the same 1961-2000 period). Is there anyone who can help me with this problem? Thank you in advance. Kind regards, Saskia van Pelt __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.