Re: [R] [External] Re: Package recommendations for outputting table with cell formatting
Blast it, *I forgot that and I usually load Hmisc as a default.* On Fri, 23 Oct 2020 at 23:24, Richard M. Heiberger wrote: > To John and everyone else, > > Please add Hmisc::latex to your how-to-make-beautiful-tables-in-r > document. > We first included latex() in S in 1995, and it is still actively > maintained and improved. > > Rich > > Here are the first few lines of ?latex > > Convert an S object to LaTeX, and Related Utilities > > Description: > > ‘latex’ converts its argument to a ‘.tex’ file appropriate for > inclusion in a LaTeX2e document. ‘latex’ is a generic function > that calls one of ‘latex.default’, ‘latex.function’, ‘latex.list’. > > ‘latex.default’ does appropriate rounding and decimal alignment > and produces a file containing a LaTeX tabular environment to > print the matrix or data.frame ‘x’ as a table. > > ‘latex.function’ prepares an S function for printing by issuing > ‘sed’ commands that are similar to those in the ‘S.to.latex’ > procedure in the ‘s.to.latex’ package (Chambers and Hastie, 1993). > ‘latex.function’ can also produce ‘verbatim’ output or output that > works with the ‘Sweavel’ LaTeX style at http://biostat.mc.vanderbilt.edu/SweaveTemplate>. > > > On Fri, Oct 23, 2020 at 11:11 PM John Kane wrote: > > > > This comes with no guarantee since i have only tried one or two packages > > mentioned but have a look at https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/;> > R > > Tables. > > > > > > On Fri, 23 Oct 2020 at 20:28, Dennis Fisher > wrote: > > > > > R 4.0.2 > > > OS X > > > > > > Colleagues > > > > > > I have the unfortunate need to create a large number of tables > (destined > > > for a Word document). I need to color cells depending on the contents, > > > e.g., blue if the value is < 0.5, red if the value is > 1.5. > > > If the output went initially to Excel, that would work; outputting > > > directly to Word would be even better. > > > I expect that several packages can accomplish this. I am looking for > > > recommendations as to which package (or combination) of packages is > best to > > > accomplish this. > > > > > > Dennis > > > > > > Dennis Fisher MD > > > P < (The "P Less Than" Company) > > > Phone / Fax: 1-866-PLessThan (1-866-753-7784) > > > www.PLessThan.com > > > > > > __ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > 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. > > > > > > > > > -- > > John Kane > > Kingston ON Canada > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > -- John Kane Kingston ON Canada [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] [External] Re: Package recommendations for outputting table with cell formatting
That latex() function is the best thing since sliced bread, Coca Cola, etc.! Thanks On Fri, Oct 23, 2020 at 9:24 PM Richard M. Heiberger wrote: > To John and everyone else, > > Please add Hmisc::latex to your how-to-make-beautiful-tables-in-r > document. > We first included latex() in S in 1995, and it is still actively > maintained and improved. > > Rich > > Here are the first few lines of ?latex > > Convert an S object to LaTeX, and Related Utilities > > Description: > > ‘latex’ converts its argument to a ‘.tex’ file appropriate for > inclusion in a LaTeX2e document. ‘latex’ is a generic function > that calls one of ‘latex.default’, ‘latex.function’, ‘latex.list’. > > ‘latex.default’ does appropriate rounding and decimal alignment > and produces a file containing a LaTeX tabular environment to > print the matrix or data.frame ‘x’ as a table. > > ‘latex.function’ prepares an S function for printing by issuing > ‘sed’ commands that are similar to those in the ‘S.to.latex’ > procedure in the ‘s.to.latex’ package (Chambers and Hastie, 1993). > ‘latex.function’ can also produce ‘verbatim’ output or output that > works with the ‘Sweavel’ LaTeX style at http://biostat.mc.vanderbilt.edu/SweaveTemplate>. > > > On Fri, Oct 23, 2020 at 11:11 PM John Kane wrote: > > > > This comes with no guarantee since i have only tried one or two packages > > mentioned but have a look at https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/;> > R > > Tables. > > > > > > On Fri, 23 Oct 2020 at 20:28, Dennis Fisher > wrote: > > > > > R 4.0.2 > > > OS X > > > > > > Colleagues > > > > > > I have the unfortunate need to create a large number of tables > (destined > > > for a Word document). I need to color cells depending on the contents, > > > e.g., blue if the value is < 0.5, red if the value is > 1.5. > > > If the output went initially to Excel, that would work; outputting > > > directly to Word would be even better. > > > I expect that several packages can accomplish this. I am looking for > > > recommendations as to which package (or combination) of packages is > best to > > > accomplish this. > > > > > > Dennis > > > > > > Dennis Fisher MD > > > P < (The "P Less Than" Company) > > > Phone / Fax: 1-866-PLessThan (1-866-753-7784) > > > www.PLessThan.com > > > > > > __ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > 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. > > > > > > > > > -- > > John Kane > > Kingston ON Canada > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > -- Erin Hodgess, PhD mailto: erinm.hodg...@gmail.com [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] [External] Re: Package recommendations for outputting table with cell formatting
To John and everyone else, Please add Hmisc::latex to your how-to-make-beautiful-tables-in-r document. We first included latex() in S in 1995, and it is still actively maintained and improved. Rich Here are the first few lines of ?latex Convert an S object to LaTeX, and Related Utilities Description: ‘latex’ converts its argument to a ‘.tex’ file appropriate for inclusion in a LaTeX2e document. ‘latex’ is a generic function that calls one of ‘latex.default’, ‘latex.function’, ‘latex.list’. ‘latex.default’ does appropriate rounding and decimal alignment and produces a file containing a LaTeX tabular environment to print the matrix or data.frame ‘x’ as a table. ‘latex.function’ prepares an S function for printing by issuing ‘sed’ commands that are similar to those in the ‘S.to.latex’ procedure in the ‘s.to.latex’ package (Chambers and Hastie, 1993). ‘latex.function’ can also produce ‘verbatim’ output or output that works with the ‘Sweavel’ LaTeX style at http://biostat.mc.vanderbilt.edu/SweaveTemplate>. On Fri, Oct 23, 2020 at 11:11 PM John Kane wrote: > > This comes with no guarantee since i have only tried one or two packages > mentioned but have a look at https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/;> R > Tables. > > > On Fri, 23 Oct 2020 at 20:28, Dennis Fisher wrote: > > > R 4.0.2 > > OS X > > > > Colleagues > > > > I have the unfortunate need to create a large number of tables (destined > > for a Word document). I need to color cells depending on the contents, > > e.g., blue if the value is < 0.5, red if the value is > 1.5. > > If the output went initially to Excel, that would work; outputting > > directly to Word would be even better. > > I expect that several packages can accomplish this. I am looking for > > recommendations as to which package (or combination) of packages is best to > > accomplish this. > > > > Dennis > > > > Dennis Fisher MD > > P < (The "P Less Than" Company) > > Phone / Fax: 1-866-PLessThan (1-866-753-7784) > > www.PLessThan.com > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > > > > -- > John Kane > Kingston ON Canada > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Package recommendations for outputting table with cell formatting
This comes with no guarantee since i have only tried one or two packages mentioned but have a look at https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/;> R Tables. On Fri, 23 Oct 2020 at 20:28, Dennis Fisher wrote: > R 4.0.2 > OS X > > Colleagues > > I have the unfortunate need to create a large number of tables (destined > for a Word document). I need to color cells depending on the contents, > e.g., blue if the value is < 0.5, red if the value is > 1.5. > If the output went initially to Excel, that would work; outputting > directly to Word would be even better. > I expect that several packages can accomplish this. I am looking for > recommendations as to which package (or combination) of packages is best to > accomplish this. > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone / Fax: 1-866-PLessThan (1-866-753-7784) > www.PLessThan.com > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > -- John Kane Kingston ON Canada [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Package recommendations for outputting table with cell formatting
Patrick Doing this in Excel is certainly an option. But I would like to learn how to do it in R. Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone / Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com > On Oct 23, 2020, at 6:15 PM, Patrick (Malone Quantitative) > wrote: > > Will this need to be done many times? If not, it might be just as easy > to apply conditional formatting to cells after they're already in > Excel. > > On Fri, Oct 23, 2020 at 8:28 PM Dennis Fisher wrote: >> >> R 4.0.2 >> OS X >> >> Colleagues >> >> I have the unfortunate need to create a large number of tables (destined for >> a Word document). I need to color cells depending on the contents, e.g., >> blue if the value is < 0.5, red if the value is > 1.5. >> If the output went initially to Excel, that would work; outputting directly >> to Word would be even better. >> I expect that several packages can accomplish this. I am looking for >> recommendations as to which package (or combination) of packages is best to >> accomplish this. >> >> Dennis >> >> Dennis Fisher MD >> P < (The "P Less Than" Company) >> Phone / Fax: 1-866-PLessThan (1-866-753-7784) >> www.PLessThan.com >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > > > -- > Patrick S. Malone, Ph.D., Malone Quantitative > NEW Service Models: http://malonequantitative.com > > He/Him/His __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Package recommendations for outputting table with cell formatting
Will this need to be done many times? If not, it might be just as easy to apply conditional formatting to cells after they're already in Excel. On Fri, Oct 23, 2020 at 8:28 PM Dennis Fisher wrote: > > R 4.0.2 > OS X > > Colleagues > > I have the unfortunate need to create a large number of tables (destined for > a Word document). I need to color cells depending on the contents, e.g., > blue if the value is < 0.5, red if the value is > 1.5. > If the output went initially to Excel, that would work; outputting directly > to Word would be even better. > I expect that several packages can accomplish this. I am looking for > recommendations as to which package (or combination) of packages is best to > accomplish this. > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone / Fax: 1-866-PLessThan (1-866-753-7784) > www.PLessThan.com > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Patrick S. Malone, Ph.D., Malone Quantitative NEW Service Models: http://malonequantitative.com He/Him/His __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Package recommendations for outputting table with cell formatting
R 4.0.2 OS X Colleagues I have the unfortunate need to create a large number of tables (destined for a Word document). I need to color cells depending on the contents, e.g., blue if the value is < 0.5, red if the value is > 1.5. If the output went initially to Excel, that would work; outputting directly to Word would be even better. I expect that several packages can accomplish this. I am looking for recommendations as to which package (or combination) of packages is best to accomplish this. Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone / Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] 3d plot of earth with cut
Good to hear you've made such progress. Just a couple of comments: - You should use points3d() rather than rgl.points(). The latter is a low level function that may have unpleasant side effects, especially mixing it with other *3d() functions like persp3d(). - There are several ways to draw a flat surface to illustrate your data. Which one to use really depends on the form of data. With randomly distributed points, yours is as good as any. If the values are a known function of location, there are probably better ones. Duncan Murdoch On 23/10/2020 12:18 p.m., Balint Radics wrote: Dear All, Thanks a lot for the useful help again. I manage to get it done up to a point where I think I just need to apply some smoothing/interpolation to get denser points, to make it nice. Basically, I started from Duncen's script to visualize and make the clipping along a plane at a slice. Then I map my data points' values to a color palette and just plot them as points on this plane. Since I have already the (x,y,z) coordinates for my points in the slice's plane I just plot them directly. I copied the code below.. To make it nicer would be able to make a real "smooth" map on the 2D surface, rather than plotting all points (e.g. using polygons?). Best, Balint # Construct a plane at a given longitude r <- 6378.1 # radius of Earth in km fixlong <- 10.0*pi/180.0 # The longitude slice # Construct a plane in 3D: # Let vec(P0) = (P0x, P0y, P0z) be a point given in the plane of the longitude # let vec(n) = (nx, ny, nz) an orthogonal vector to this plane # then vec(P) = (Px, Py, Pz) will be in the plane if (vec(P) - vec(P0)) * vec(n) = 0 # We pick 2 arbitrary vectors in the plane out of 3 points p0x <- r*cos(2)*cos(fixlong) p0y <- r*cos(2)*sin(fixlong) p0z <- r*sin(2) p1x <- r*cos(2.5)*cos(fixlong) p1y <- r*cos(2.5)*sin(fixlong) p1z <- r*sin(2.5) p2x <- r*cos(3)*cos(fixlong) p2y <- r*cos(3)*sin(fixlong) p2z <- r*sin(3) # Make the vectors pointing to P and P0 v1x <- p1x - p0x # P v1y <- p1y - p0y v1z <- p1z - p0z v2x <- p2x - p0x # P0 v2y <- p2y - p0y v2z <- p2z - p0z # The cross product will give a vector orthogonal to the plane, (nx, ny, nz) nx <- v1y*v2z - v1z*v2y; ny <- v1z*v2x - v1x*v2z; nz <- v1x*v2y - v1y*v2x; # normalize nMag <- sqrt(nx*nx + ny*ny + nz*nz); nx <- nx / nMag; ny <- ny / nMag; nz <- nz / nMag; # Plane equation (vec(P) - vec(P0)) * vec(n) = 0, with P=(x, y, z), P0=(x0, y0, z0), # giving a*(x-x0)+b*(y-y0)+c*(z-z0) = 0, where x,x0 are two points in the plane # a, b, c are the normal vector coordinates a <- -nx b <- -ny c <- -nz d <- -(a*v2x + b*v2y + c*v2z ) open3d() # Plot the globe - from Duncan # points of a sphere lat <- matrix(seq(90, -90, len = 50)*pi/180, 50, 50, byrow = TRUE) long <- matrix(seq(-180, 180, len = 50)*pi/180, 50, 50) x <- r*cos(lat)*cos(long) y <- r*cos(lat)*sin(long) z <- r*sin(lat) # Plot with texture ids <- persp3d(x, y, z, col = "white", texture = system.file("textures/world.png", package = "rgl"), specular = "black", axes = FALSE, box = FALSE, xlab = "", ylab = "", zlab = "", normal_x = x, normal_y = y, normal_z = z) # Plot the plane across the longitude slice #planes3d(a, b, c, d, alpha = 0.6) # optionally visualize the plane # Apply clipping to only one side of the plane using the normal vector clipplanes3d(a, b, c, d) # Map something onto this plane - how? Let's try with rgl.points and mapping the colors # The data is: data_activity and variables are $X, $Y, $Z, $Ar library(leaflet) # map the colors to the data values pal <- colorNumeric( palette = "Blues", domain = data_activity$Ar) # # plot the points and the mapped colors rgl.points( data_activity$X, data_activity$Y, data_activity$Z, color = pal(data_activity$Ar), size=3) On Fri, Oct 23, 2020 at 1:50 AM aBBy Spurdle, ⍺XY wrote: It should be a 2D slice/plane embedded into a 3D space. I was able to come up with the plot, attached. My intention was to plot national boundaries on the surface of a sphere. And put the slice inside. However, I haven't (as yet) worked out how to get the coordinates for the boundaries. Let me know, if of any value. And I'll post the code. (But needs to be polished first) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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,
Re: [R] formula mungeing
I can't compete with Gabor's "elegant" solution, but I don't understand why your original "ugly" approach doesn't work. g <- function(lambdas, f){ z <- as.character(f) for(i in seq_along(lambdas)) z[3]<- sub(paste0("lambdas[", i, "]"), lambdas[i], z[3], fixed = TRUE) formula(paste0(z[c(2,1,3)], collapse = " ")) } > f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + s > g(1:2, f) y ~ qss(x, lambda = 1) + qss(z, 2) + s > f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + + qss(z,lambdas[3]) + s > g(3:1, f) y ~ qss(x, lambda = 3) + qss(z, 2) + qss(z, 1) + s Is there maybe a problem with the environment of the created formula? Or have I misunderstood what you wanted? Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Oct 23, 2020 at 6:33 AM Koenker, Roger W wrote: > Suppose I have a formula like this: > > f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + s > > I’d like a function, g(lambdas, f) that would take g(c(2,3), f) and > produce the new > formula: > > y ~ qss(x, lambda = 2) + qss(z, 3) + s > > For only two qss terms I have been using > > g <- function(lambdas, f){ > F <- deparse(f) > F <- gsub("lambdas\\[1\\]",lambdas[1],F) > F <- gsub("lambdas\\[2\\]",lambdas[2],F) > formula(F) > } > but this is ugly and doesn’t extend nicely to more qss terms. Isn’t there > some > bquote() magic that can be invoked? Or something else entirely? > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Extracting data and saving in excel
Hello, First coerce the column birth to class Date, then subset. data$birth <- as.Date(data$birth) i <- data$birth > as.Date("1979-01-01") subdata <- data[i, ] To write back to Excel, use write.csv or a package able to write Excel files directly. Hope this helps, Rui Barradas Às 12:23 de 23/10/20, bgnumis bgnum escreveu: Hi all, I have a dataframe call "data", and have lots of rows. One of them is data$`birth` my data has been imported from an excel file in RSTUDIO I want to obtain in a function all rows (with the names) where data$`birth` is bigger than 1979/01/01 and extract in a excel file as an output (a sub-excel of the original). Can you guide with the functions I need to use? I have been many time out of programming and I need a bit of help to get in fit. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] 3d plot of earth with cut
Dear All, Thanks a lot for the useful help again. I manage to get it done up to a point where I think I just need to apply some smoothing/interpolation to get denser points, to make it nice. Basically, I started from Duncen's script to visualize and make the clipping along a plane at a slice. Then I map my data points' values to a color palette and just plot them as points on this plane. Since I have already the (x,y,z) coordinates for my points in the slice's plane I just plot them directly. I copied the code below.. To make it nicer would be able to make a real "smooth" map on the 2D surface, rather than plotting all points (e.g. using polygons?). Best, Balint # Construct a plane at a given longitude r <- 6378.1 # radius of Earth in km fixlong <- 10.0*pi/180.0 # The longitude slice # Construct a plane in 3D: # Let vec(P0) = (P0x, P0y, P0z) be a point given in the plane of the longitude # let vec(n) = (nx, ny, nz) an orthogonal vector to this plane # then vec(P) = (Px, Py, Pz) will be in the plane if (vec(P) - vec(P0)) * vec(n) = 0 # We pick 2 arbitrary vectors in the plane out of 3 points p0x <- r*cos(2)*cos(fixlong) p0y <- r*cos(2)*sin(fixlong) p0z <- r*sin(2) p1x <- r*cos(2.5)*cos(fixlong) p1y <- r*cos(2.5)*sin(fixlong) p1z <- r*sin(2.5) p2x <- r*cos(3)*cos(fixlong) p2y <- r*cos(3)*sin(fixlong) p2z <- r*sin(3) # Make the vectors pointing to P and P0 v1x <- p1x - p0x # P v1y <- p1y - p0y v1z <- p1z - p0z v2x <- p2x - p0x # P0 v2y <- p2y - p0y v2z <- p2z - p0z # The cross product will give a vector orthogonal to the plane, (nx, ny, nz) nx <- v1y*v2z - v1z*v2y; ny <- v1z*v2x - v1x*v2z; nz <- v1x*v2y - v1y*v2x; # normalize nMag <- sqrt(nx*nx + ny*ny + nz*nz); nx <- nx / nMag; ny <- ny / nMag; nz <- nz / nMag; # Plane equation (vec(P) - vec(P0)) * vec(n) = 0, with P=(x, y, z), P0=(x0, y0, z0), # giving a*(x-x0)+b*(y-y0)+c*(z-z0) = 0, where x,x0 are two points in the plane # a, b, c are the normal vector coordinates a <- -nx b <- -ny c <- -nz d <- -(a*v2x + b*v2y + c*v2z ) open3d() # Plot the globe - from Duncan # points of a sphere lat <- matrix(seq(90, -90, len = 50)*pi/180, 50, 50, byrow = TRUE) long <- matrix(seq(-180, 180, len = 50)*pi/180, 50, 50) x <- r*cos(lat)*cos(long) y <- r*cos(lat)*sin(long) z <- r*sin(lat) # Plot with texture ids <- persp3d(x, y, z, col = "white", texture = system.file("textures/world.png", package = "rgl"), specular = "black", axes = FALSE, box = FALSE, xlab = "", ylab = "", zlab = "", normal_x = x, normal_y = y, normal_z = z) # Plot the plane across the longitude slice #planes3d(a, b, c, d, alpha = 0.6) # optionally visualize the plane # Apply clipping to only one side of the plane using the normal vector clipplanes3d(a, b, c, d) # Map something onto this plane - how? Let's try with rgl.points and mapping the colors # The data is: data_activity and variables are $X, $Y, $Z, $Ar library(leaflet) # map the colors to the data values pal <- colorNumeric( palette = "Blues", domain = data_activity$Ar) # # plot the points and the mapped colors rgl.points( data_activity$X, data_activity$Y, data_activity$Z, color = pal(data_activity$Ar), size=3) On Fri, Oct 23, 2020 at 1:50 AM aBBy Spurdle, ⍺XY wrote: > > It should be a 2D slice/plane embedded into a 3D space. > > I was able to come up with the plot, attached. > My intention was to plot national boundaries on the surface of a sphere. > And put the slice inside. > However, I haven't (as yet) worked out how to get the coordinates for > the boundaries. > > Let me know, if of any value. > And I'll post the code. > (But needs to be polished first) > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
Hello, What about this? straightline <- function(x, slope, intercept) slope*x + intercept p <- ggplot(data = trainset, aes(x=x, y=y, color=z)) + geom_point() + geom_ribbon(aes(ymin = straightline(x, slope_1, intercept_1 + 1/w[2]), ymax = straightline(x, slope_1, intercept_1 - 1/w[2])), color = NA, fill = "grey", alpha = 0.5) + geom_abline(slope = slope_1, intercept = intercept_1) + scale_color_manual(values = c("red", "blue")) p Note that now the middle line is drawn last. Otherwise it will be greyed by the ribbon. Hope this helps, Rui Barradas Às 08:58 de 23/10/20, Luigi Marongiu escreveu: Hello, I am running SVM and showing the results with ggplot2. The results include the decision boundaries, which are two dashed lines parallel to a solid line. I would like to remove the dashed lines and use a shaded area instead. How can I do that? Here is the code I wrote.. ``` library(e1071) library(ggplot2) set.seed(100) x1 = rnorm(100, mean = 0.2, sd = 0.1) y1 = rnorm(100, mean = 0.7, sd = 0.1) y2 = rnorm(100, mean = 0.2, sd = 0.1) x2 = rnorm(100, mean = 0.75, sd = 0.1) df = data.frame(x = c(x1,x2), y=c(y1,y2), z=c(rep(0, length(x1)), rep(1, length(x2 df$z = factor(c(rep(0, length(x1)), rep(1, length(x2 df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0) trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum <- grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- testset[, -trainColNum] head(trainset); str(df) svm_model<- svm(z ~ ., data = trainset, type = "C-classification", kernel = "linear", scale = FALSE) #! plot p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + geom_point() + scale_color_manual(values = c("red", "blue")) # show decision boundaries w = t(svm_model$coefs) %*% svm_model$SV # %*% = matrix multiplication slope_1 = -w[1]/w[2] intercept_1 = svm_model$rho / w[2] p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### here we go: can I use a shaded area between these two lines? ### p = p + geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], linetype = "dashed") + geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], linetype = "dashed") print(p) ``` Thank you __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] [R-pkgs] openSTARS version 1.2.2 on CRAN
Dear R community There is a new version of the openSTARS package (preparing GIS data for stream analysis) available on CRAN. Major updates are: - catch errors arising in GRASS 7.8 due to changes in v.to.db - update setup_grass_environment; there is no need any more to initGRASS before Please also find a detailed description of the package including example code here: https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0239237 Any comments, suggestions or bug reports are welcome on https://github.com/MiKatt/openSTARS All the best, Mira -- Dr. Mira Kattwinkel Quantitative Landscape Ecology iES Landau, Institute for Environmental Sciences University of Koblenz-Landau Fortstraße 7 76829 Landau Germany Phone: + 49 6341 280-31553 Office: Building I, Room 2.02 ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Extracting data and saving in excel
Hi all, I have a dataframe call "data", and have lots of rows. One of them is data$`birth` my data has been imported from an excel file in RSTUDIO I want to obtain in a function all rows (with the names) where data$`birth` is bigger than 1979/01/01 and extract in a excel file as an output (a sub-excel of the original). Can you guide with the functions I need to use? I have been many time out of programming and I need a bit of help to get in fit. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] R & ACLED API
r-help Forum Has anyone worked with puling ACLED (https://acleddata.com/#/dashboard) via the ACLED API and R? Jeff Reichman [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] formula mungeing
Thanks, Gabor! Very elegant! > On Oct 23, 2020, at 4:15 PM, Gabor Grothendieck > wrote: > > Recursively walk the formula performing the replacement: > > g <- function(e, ...) { >if (length(e) > 1) { >if (identical(e[[2]], as.name(names(list(...) { > e <- eval(e, list(...)) >} >if (length(e) > 1) for (i in 1:length(e)) e[[i]] <- Recall(e[[i]], ...) >} >e > } > > g(f, lambdas = 2:3) > ## y ~ qss(x, lambda = 2L) + qss(z, 3L) + s > > On Fri, Oct 23, 2020 at 9:33 AM Koenker, Roger W > wrote: >> >> Suppose I have a formula like this: >> >>f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + s >> >> I’d like a function, g(lambdas, f) that would take g(c(2,3), f) and produce >> the new >> formula: >> >>y ~ qss(x, lambda = 2) + qss(z, 3) + s >> >> For only two qss terms I have been using >> >>g <- function(lambdas, f){ >>F <- deparse(f) >>F <- gsub("lambdas\\[1\\]",lambdas[1],F) >>F <- gsub("lambdas\\[2\\]",lambdas[2],F) >>formula(F) >>} >> but this is ugly and doesn’t extend nicely to more qss terms. Isn’t there >> some >> bquote() magic that can be invoked? Or something else entirely? >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] formula mungeing
Recursively walk the formula performing the replacement: g <- function(e, ...) { if (length(e) > 1) { if (identical(e[[2]], as.name(names(list(...) { e <- eval(e, list(...)) } if (length(e) > 1) for (i in 1:length(e)) e[[i]] <- Recall(e[[i]], ...) } e } g(f, lambdas = 2:3) ## y ~ qss(x, lambda = 2L) + qss(z, 3L) + s On Fri, Oct 23, 2020 at 9:33 AM Koenker, Roger W wrote: > > Suppose I have a formula like this: > > f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + s > > I’d like a function, g(lambdas, f) that would take g(c(2,3), f) and produce > the new > formula: > > y ~ qss(x, lambda = 2) + qss(z, 3) + s > > For only two qss terms I have been using > > g <- function(lambdas, f){ > F <- deparse(f) > F <- gsub("lambdas\\[1\\]",lambdas[1],F) > F <- gsub("lambdas\\[2\\]",lambdas[2],F) > formula(F) > } > but this is ugly and doesn’t extend nicely to more qss terms. Isn’t there > some > bquote() magic that can be invoked? Or something else entirely? > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] formula mungeing
Suppose I have a formula like this: f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + s I’d like a function, g(lambdas, f) that would take g(c(2,3), f) and produce the new formula: y ~ qss(x, lambda = 2) + qss(z, 3) + s For only two qss terms I have been using g <- function(lambdas, f){ F <- deparse(f) F <- gsub("lambdas\\[1\\]",lambdas[1],F) F <- gsub("lambdas\\[2\\]",lambdas[2],F) formula(F) } but this is ugly and doesn’t extend nicely to more qss terms. Isn’t there some bquote() magic that can be invoked? Or something else entirely? __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
Thank you, but this split the area into two and distorts the shape of the plot. (compared to ``` p + geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], linetype = "dashed", col = "royalblue") + geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], linetype = "dashed", col = "royalblue") ``` Why there is a hole in the middle of the ribbon? and the color is not grey... On Fri, Oct 23, 2020 at 2:35 PM PIKAL Petr wrote: > > Hi > > What about something like > > p+geom_ribbon(aes(ymin = slope_1*x + intercept_1 - 1/w[2], > ymax = slope_1*x + intercept_1 + 1/w[2], fill = "grey70", alpha=0.1)) > > Cheers > Petr > > > -Original Message- > > From: Luigi Marongiu > > Sent: Friday, October 23, 2020 11:11 AM > > To: PIKAL Petr > > Cc: r-help > > Subject: Re: [R] How to shade area between lines in ggplot2 > > > > also from this site: https://plotly.com/ggplot2/geom_ribbon/ > > I get the answer is geom_ribbon but I am still missing something ``` #! plot > > p > > = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > support vectors df_sv = trainset[svm_model$index, ] p = p + > > geom_point(data = df_sv, aes(x=x, y=y), > >color="purple", size=4, alpha=0.5) # show hyperplane > > (decision > > boundaries are off set by 1/w[2]) w = t(svm_model$coefs) %*% > > svm_model$SV # %*% = matrix multiplication > > slope_1 = -w[1]/w[2] > > intercept_1 = svm_model$rho / w[2] > > p = p + geom_abline(slope = slope_1, intercept = intercept_1, col = > > "royalblue4") > > p = p + geom_ribbon(aes(ymin=intercept_1 - 1/w[2], > > ymax=intercept_1 + 1/w[2], > > x=x, fill = "band"), alpha = 0.3) + > > scale_fill_manual("",values="grey12") > > ``` > > > > On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr wrote: > > > > > > Hi > > > > > > Did you try google? I got several answers using your question > > > > > > e.g. > > > https://stackoverflow.com/questions/54687321/fill-area-between-lines-u > > > sing-g > > > gplot-in-r > > > > > > Cheers > > > Petr > > > > > > > -Original Message- > > > > From: R-help On Behalf Of Luigi > > > > Marongiu > > > > Sent: Friday, October 23, 2020 9:59 AM > > > > To: r-help > > > > Subject: [R] How to shade area between lines in ggplot2 > > > > > > > > Hello, > > > > I am running SVM and showing the results with ggplot2. The results > > > > include the decision boundaries, which are two dashed lines parallel > > > > to a solid > > > line. I > > > > would like to remove the dashed lines and use a shaded area instead. > > > > How can I do that? > > > > Here is the code I wrote.. > > > > ``` > > > > library(e1071) > > > > library(ggplot2) > > > > > > > > set.seed(100) > > > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > > > x2 = rnorm(100, mean = 0.75, sd = 0.1) df = data.frame(x = c(x1,x2), > > > > y=c(y1,y2), > > > > z=c(rep(0, length(x1)), rep(1, length(x2 df$z = > > > factor(c(rep(0, > > > > length(x1)), rep(1, length(x2 df[, "train"] <- > > > > ifelse(runif(nrow(df)) > > > < 0.8, 1, 0) > > > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] > > > > trainColNum > > > <- > > > > grep("train", names(df)) trainset <- trainset[, -trainColNum] > > > > testset <- > > > testset[, > > > > -trainColNum] head(trainset); str(df) > > > > > > > > svm_model<- svm(z ~ ., > > > > data = trainset, > > > > type = "C-classification", > > > > kernel = "linear", > > > > scale = FALSE) > > > > > > > > #! plot > > > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > > > geom_point() + scale_color_manual(values = c("red", "blue")) # > > > > show decision boundaries w = t(svm_model$coefs) %*% svm_model$SV > > # > > > > %*% = matrix multiplication > > > > slope_1 = -w[1]/w[2] > > > > intercept_1 = svm_model$rho / w[2] > > > > p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### > > > > here we > > > go: > > > > can I use a shaded area between these two lines? ### p = p + > > > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > > > linetype = "dashed") + > > > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > > > linetype = "dashed") > > > > print(p) > > > > > > > > ``` > > > > > > > > Thank you > > > > > > > > -- > > > > Best regards, > > > > Luigi > > > > > > > > __ > > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > > 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. > > > > > > > > -- > > Best
[R] [R-pkgs] WriteXLS Version 6.0.0 Released
Hi All, WriteXLS version 6.0.0 has been released and is available on CRAN. The key changes are: 1. More robust handling of embedded newline ('\n') and carriage return ('\r') characters, double quotes, and two character sequences of a backslash character ('\') followed by another character, within a data value, has been implemented. An indirect consequence of this more robust handling, is better support for multiline cells in the resultant Excel worksheet. 2. New 'AllText' argument to globally handle/overcome Excel's default reformatting of erstwhile numeric values with leading and/or trailing zeros. This option enhances prior improvements to preserving leading and/or trailing zeros on a worksheet cell specific basis. 3. Updated Perl libraries are included as apropos. Regards, Marc Schwartz ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
Hi What about something like p+geom_ribbon(aes(ymin = slope_1*x + intercept_1 - 1/w[2], ymax = slope_1*x + intercept_1 + 1/w[2], fill = "grey70", alpha=0.1)) Cheers Petr > -Original Message- > From: Luigi Marongiu > Sent: Friday, October 23, 2020 11:11 AM > To: PIKAL Petr > Cc: r-help > Subject: Re: [R] How to shade area between lines in ggplot2 > > also from this site: https://plotly.com/ggplot2/geom_ribbon/ > I get the answer is geom_ribbon but I am still missing something ``` #! plot > p > = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > geom_point() + scale_color_manual(values = c("red", "blue")) # show > support vectors df_sv = trainset[svm_model$index, ] p = p + > geom_point(data = df_sv, aes(x=x, y=y), >color="purple", size=4, alpha=0.5) # show hyperplane > (decision > boundaries are off set by 1/w[2]) w = t(svm_model$coefs) %*% > svm_model$SV # %*% = matrix multiplication > slope_1 = -w[1]/w[2] > intercept_1 = svm_model$rho / w[2] > p = p + geom_abline(slope = slope_1, intercept = intercept_1, col = > "royalblue4") > p = p + geom_ribbon(aes(ymin=intercept_1 - 1/w[2], > ymax=intercept_1 + 1/w[2], > x=x, fill = "band"), alpha = 0.3) + > scale_fill_manual("",values="grey12") > ``` > > On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr wrote: > > > > Hi > > > > Did you try google? I got several answers using your question > > > > e.g. > > https://stackoverflow.com/questions/54687321/fill-area-between-lines-u > > sing-g > > gplot-in-r > > > > Cheers > > Petr > > > > > -Original Message- > > > From: R-help On Behalf Of Luigi > > > Marongiu > > > Sent: Friday, October 23, 2020 9:59 AM > > > To: r-help > > > Subject: [R] How to shade area between lines in ggplot2 > > > > > > Hello, > > > I am running SVM and showing the results with ggplot2. The results > > > include the decision boundaries, which are two dashed lines parallel > > > to a solid > > line. I > > > would like to remove the dashed lines and use a shaded area instead. > > > How can I do that? > > > Here is the code I wrote.. > > > ``` > > > library(e1071) > > > library(ggplot2) > > > > > > set.seed(100) > > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > > x2 = rnorm(100, mean = 0.75, sd = 0.1) df = data.frame(x = c(x1,x2), > > > y=c(y1,y2), > > > z=c(rep(0, length(x1)), rep(1, length(x2 df$z = > > factor(c(rep(0, > > > length(x1)), rep(1, length(x2 df[, "train"] <- > > > ifelse(runif(nrow(df)) > > < 0.8, 1, 0) > > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] > > > trainColNum > > <- > > > grep("train", names(df)) trainset <- trainset[, -trainColNum] > > > testset <- > > testset[, > > > -trainColNum] head(trainset); str(df) > > > > > > svm_model<- svm(z ~ ., > > > data = trainset, > > > type = "C-classification", > > > kernel = "linear", > > > scale = FALSE) > > > > > > #! plot > > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > > geom_point() + scale_color_manual(values = c("red", "blue")) # > > > show decision boundaries w = t(svm_model$coefs) %*% svm_model$SV > # > > > %*% = matrix multiplication > > > slope_1 = -w[1]/w[2] > > > intercept_1 = svm_model$rho / w[2] > > > p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### > > > here we > > go: > > > can I use a shaded area between these two lines? ### p = p + > > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > > linetype = "dashed") + > > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > > linetype = "dashed") > > > print(p) > > > > > > ``` > > > > > > Thank you > > > > > > -- > > > Best regards, > > > Luigi > > > > > > __ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > 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. > > > > -- > Best regards, > Luigi __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
also from this site: https://plotly.com/ggplot2/geom_ribbon/ I get the answer is geom_ribbon but I am still missing something ``` #! plot p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + geom_point() + scale_color_manual(values = c("red", "blue")) # show support vectors df_sv = trainset[svm_model$index, ] p = p + geom_point(data = df_sv, aes(x=x, y=y), color="purple", size=4, alpha=0.5) # show hyperplane (decision boundaries are off set by 1/w[2]) w = t(svm_model$coefs) %*% svm_model$SV # %*% = matrix multiplication slope_1 = -w[1]/w[2] intercept_1 = svm_model$rho / w[2] p = p + geom_abline(slope = slope_1, intercept = intercept_1, col = "royalblue4") p = p + geom_ribbon(aes(ymin=intercept_1 - 1/w[2], ymax=intercept_1 + 1/w[2], x=x, fill = "band"), alpha = 0.3) + scale_fill_manual("",values="grey12") ``` On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr wrote: > > Hi > > Did you try google? I got several answers using your question > > e.g. > https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g > gplot-in-r > > Cheers > Petr > > > -Original Message- > > From: R-help On Behalf Of Luigi Marongiu > > Sent: Friday, October 23, 2020 9:59 AM > > To: r-help > > Subject: [R] How to shade area between lines in ggplot2 > > > > Hello, > > I am running SVM and showing the results with ggplot2. The results include > > the decision boundaries, which are two dashed lines parallel to a solid > line. I > > would like to remove the dashed lines and use a shaded area instead. How > > can I do that? > > Here is the code I wrote.. > > ``` > > library(e1071) > > library(ggplot2) > > > > set.seed(100) > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > x2 = rnorm(100, mean = 0.75, sd = 0.1) > > df = data.frame(x = c(x1,x2), y=c(y1,y2), > > z=c(rep(0, length(x1)), rep(1, length(x2 df$z = > factor(c(rep(0, > > length(x1)), rep(1, length(x2 df[, "train"] <- ifelse(runif(nrow(df)) > < 0.8, 1, 0) > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum > <- > > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- > testset[, > > -trainColNum] head(trainset); str(df) > > > > svm_model<- svm(z ~ ., > > data = trainset, > > type = "C-classification", > > kernel = "linear", > > scale = FALSE) > > > > #! plot > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > decision boundaries w = t(svm_model$coefs) %*% svm_model$SV # %*% = > > matrix multiplication > > slope_1 = -w[1]/w[2] > > intercept_1 = svm_model$rho / w[2] > > p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### here we > go: > > can I use a shaded area between these two lines? ### p = p + > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > linetype = "dashed") + > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > linetype = "dashed") > > print(p) > > > > ``` > > > > Thank you > > > > -- > > Best regards, > > Luigi > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. -- Best regards, Luigi __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
I tried from this website https://community.rstudio.com/t/fill-area-between-lines-using-ggplot-in-r/35355/2 it looked promising but did not work for me: ``` #! plot p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + geom_point() + scale_color_manual(values = c("red", "blue")) # show support vectors df_sv = trainset[svm_model$index, ] p = p + geom_point(data = df_sv, aes(x=x, y=y), color="purple", size=4, alpha=0.5) # show hyperplane (decision boundaries are off set by 1/w[2]) w = t(svm_model$coefs) %*% svm_model$SV # %*% = matrix multiplication slope_1 = -w[1]/w[2] intercept_1 = svm_model$rho / w[2] p = p + geom_abline(slope = slope_1, intercept = intercept_1, col = "royalblue4") p = p + geom_ribbon(aes(ymin = intercept_1 - 1/w[2], ymax = intercept_1 + 1/w[2]), fill = "grey70") ``` On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr wrote: > > Hi > > Did you try google? I got several answers using your question > > e.g. > https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g > gplot-in-r > > Cheers > Petr > > > -Original Message- > > From: R-help On Behalf Of Luigi Marongiu > > Sent: Friday, October 23, 2020 9:59 AM > > To: r-help > > Subject: [R] How to shade area between lines in ggplot2 > > > > Hello, > > I am running SVM and showing the results with ggplot2. The results include > > the decision boundaries, which are two dashed lines parallel to a solid > line. I > > would like to remove the dashed lines and use a shaded area instead. How > > can I do that? > > Here is the code I wrote.. > > ``` > > library(e1071) > > library(ggplot2) > > > > set.seed(100) > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > x2 = rnorm(100, mean = 0.75, sd = 0.1) > > df = data.frame(x = c(x1,x2), y=c(y1,y2), > > z=c(rep(0, length(x1)), rep(1, length(x2 df$z = > factor(c(rep(0, > > length(x1)), rep(1, length(x2 df[, "train"] <- ifelse(runif(nrow(df)) > < 0.8, 1, 0) > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum > <- > > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- > testset[, > > -trainColNum] head(trainset); str(df) > > > > svm_model<- svm(z ~ ., > > data = trainset, > > type = "C-classification", > > kernel = "linear", > > scale = FALSE) > > > > #! plot > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > decision boundaries w = t(svm_model$coefs) %*% svm_model$SV # %*% = > > matrix multiplication > > slope_1 = -w[1]/w[2] > > intercept_1 = svm_model$rho / w[2] > > p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### here we > go: > > can I use a shaded area between these two lines? ### p = p + > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > linetype = "dashed") + > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > linetype = "dashed") > > print(p) > > > > ``` > > > > Thank you > > > > -- > > Best regards, > > Luigi > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. -- Best regards, Luigi __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
I don't know if I can find a case similar to my need. I tried with: ``` p = p + geom_abline(slope = slope_1, intercept = intercept_1, col = "royalblue4") p = p + geom_line() + geom_hline(yintercept = 5) + theme_classic() + geom_ribbon(aes(ymin=intercept_1 - 1/w[2],ymax=intercept_1 + 1/w[2]), fill="blue") ``` but did not work. Thanks anyway On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr wrote: > > Hi > > Did you try google? I got several answers using your question > > e.g. > https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g > gplot-in-r > > Cheers > Petr > > > -Original Message- > > From: R-help On Behalf Of Luigi Marongiu > > Sent: Friday, October 23, 2020 9:59 AM > > To: r-help > > Subject: [R] How to shade area between lines in ggplot2 > > > > Hello, > > I am running SVM and showing the results with ggplot2. The results include > > the decision boundaries, which are two dashed lines parallel to a solid > line. I > > would like to remove the dashed lines and use a shaded area instead. How > > can I do that? > > Here is the code I wrote.. > > ``` > > library(e1071) > > library(ggplot2) > > > > set.seed(100) > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > x2 = rnorm(100, mean = 0.75, sd = 0.1) > > df = data.frame(x = c(x1,x2), y=c(y1,y2), > > z=c(rep(0, length(x1)), rep(1, length(x2 df$z = > factor(c(rep(0, > > length(x1)), rep(1, length(x2 df[, "train"] <- ifelse(runif(nrow(df)) > < 0.8, 1, 0) > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum > <- > > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- > testset[, > > -trainColNum] head(trainset); str(df) > > > > svm_model<- svm(z ~ ., > > data = trainset, > > type = "C-classification", > > kernel = "linear", > > scale = FALSE) > > > > #! plot > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > decision boundaries w = t(svm_model$coefs) %*% svm_model$SV # %*% = > > matrix multiplication > > slope_1 = -w[1]/w[2] > > intercept_1 = svm_model$rho / w[2] > > p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### here we > go: > > can I use a shaded area between these two lines? ### p = p + > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > linetype = "dashed") + > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > linetype = "dashed") > > print(p) > > > > ``` > > > > Thank you > > > > -- > > Best regards, > > Luigi > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. -- Best regards, Luigi __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
Hi Did you try google? I got several answers using your question e.g. https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g gplot-in-r Cheers Petr > -Original Message- > From: R-help On Behalf Of Luigi Marongiu > Sent: Friday, October 23, 2020 9:59 AM > To: r-help > Subject: [R] How to shade area between lines in ggplot2 > > Hello, > I am running SVM and showing the results with ggplot2. The results include > the decision boundaries, which are two dashed lines parallel to a solid line. I > would like to remove the dashed lines and use a shaded area instead. How > can I do that? > Here is the code I wrote.. > ``` > library(e1071) > library(ggplot2) > > set.seed(100) > x1 = rnorm(100, mean = 0.2, sd = 0.1) > y1 = rnorm(100, mean = 0.7, sd = 0.1) > y2 = rnorm(100, mean = 0.2, sd = 0.1) > x2 = rnorm(100, mean = 0.75, sd = 0.1) > df = data.frame(x = c(x1,x2), y=c(y1,y2), > z=c(rep(0, length(x1)), rep(1, length(x2 df$z = factor(c(rep(0, > length(x1)), rep(1, length(x2 df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0) > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum <- > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- testset[, > -trainColNum] head(trainset); str(df) > > svm_model<- svm(z ~ ., > data = trainset, > type = "C-classification", > kernel = "linear", > scale = FALSE) > > #! plot > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > geom_point() + scale_color_manual(values = c("red", "blue")) # show > decision boundaries w = t(svm_model$coefs) %*% svm_model$SV # %*% = > matrix multiplication > slope_1 = -w[1]/w[2] > intercept_1 = svm_model$rho / w[2] > p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### here we go: > can I use a shaded area between these two lines? ### p = p + > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > linetype = "dashed") + > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > linetype = "dashed") > print(p) > > ``` > > Thank you > > -- > Best regards, > Luigi > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] How to shade area between lines in ggplot2
Hello, I am running SVM and showing the results with ggplot2. The results include the decision boundaries, which are two dashed lines parallel to a solid line. I would like to remove the dashed lines and use a shaded area instead. How can I do that? Here is the code I wrote.. ``` library(e1071) library(ggplot2) set.seed(100) x1 = rnorm(100, mean = 0.2, sd = 0.1) y1 = rnorm(100, mean = 0.7, sd = 0.1) y2 = rnorm(100, mean = 0.2, sd = 0.1) x2 = rnorm(100, mean = 0.75, sd = 0.1) df = data.frame(x = c(x1,x2), y=c(y1,y2), z=c(rep(0, length(x1)), rep(1, length(x2 df$z = factor(c(rep(0, length(x1)), rep(1, length(x2 df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0) trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum <- grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- testset[, -trainColNum] head(trainset); str(df) svm_model<- svm(z ~ ., data = trainset, type = "C-classification", kernel = "linear", scale = FALSE) #! plot p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + geom_point() + scale_color_manual(values = c("red", "blue")) # show decision boundaries w = t(svm_model$coefs) %*% svm_model$SV # %*% = matrix multiplication slope_1 = -w[1]/w[2] intercept_1 = svm_model$rho / w[2] p = p + geom_abline(slope = slope_1, intercept = intercept_1) ### here we go: can I use a shaded area between these two lines? ### p = p + geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], linetype = "dashed") + geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], linetype = "dashed") print(p) ``` Thank you -- Best regards, Luigi __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.