I am having trouble with a gradient fill application in ggplot2, caused
by outlier values. In my reprex, most of the values are between 2 and -2, but there are two outliers, 10 and -15. The outliers stand out well, which is good, but all the other numbers show almost no colour variation. I would like to continue with bold colours for the outliers, while having a more variable gradient, perhaps but not necessarily in a different colour, for the values between 2 nd -2. Any ideas on how this can be done? library(ggplot2) a <- c(rep(1,6),rep(2,6),rep(3,6),rep(4,6)) b <- c(0.1, 0.5,-0.3, 1.2,-0.4,-1.2, 0.7, 0.8,-1.2,-0.5,10.0, 0.3, 0.2,-0.4,-15.,-0.4,-0.9, NA, 0.1, 1.3,-1.4, 0.5,-0.5, 0.1) c <- c(rep(c("a","b","c","d","e","f"),4)) df <- data.frame(a,b) ggplot(df,aes(x=a,y=c,fill=b))+ geom_tile()+ geom_text(label=paste0(round(b,1),"%"), size=10,colour="gold")+ scale_fill_gradient(low="red",high="blue",na.value="grey50") ______________________________________________ [hidden email] 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. |
Perhaps scale_fill_gradientn() would be useful.
On March 8, 2021 8:05:52 PM PST, [hidden email] wrote: >I am having trouble with a gradient fill application in ggplot2, caused > >by outlier values. In my reprex, most of the values are between 2 and >-2, but there are two outliers, 10 and -15. The outliers stand out >well, >which is good, but all the other numbers show almost no colour >variation. I would like to continue with bold colours for the outliers, > >while having a more variable gradient, perhaps but not necessarily in a > >different colour, for the values between 2 nd -2. Any ideas on how this > >can be done? > > >library(ggplot2) >a <- c(rep(1,6),rep(2,6),rep(3,6),rep(4,6)) >b <- c(0.1, 0.5,-0.3, 1.2,-0.4,-1.2, > 0.7, 0.8,-1.2,-0.5,10.0, 0.3, > 0.2,-0.4,-15.,-0.4,-0.9, NA, > 0.1, 1.3,-1.4, 0.5,-0.5, 0.1) >c <- c(rep(c("a","b","c","d","e","f"),4)) >df <- data.frame(a,b) >ggplot(df,aes(x=a,y=c,fill=b))+ > geom_tile()+ > geom_text(label=paste0(round(b,1),"%"), > size=10,colour="gold")+ > scale_fill_gradient(low="red",high="blue",na.value="grey50") > >______________________________________________ >[hidden email] 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. -- Sent from my phone. Please excuse my brevity. ______________________________________________ [hidden email] 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. |
In reply to this post by phil-3
Hi Phillip.
You could try some transformations of your data. Either directly in scale_fill_gradient or in original data. log transition would be OK but not with negative data. You could play with some transitions from scales package. I think that you could also use rescaler function, but I do not know how. ggplot(df,aes(x=a,y=c,fill=b))+ geom_tile()+ geom_text(label=paste0(round(b,1),"%"), size=10,colour="gold")+ scale_fill_gradient(low="red",high="blue",na.value="grey50", trans="log") Cheers. Petr > -----Original Message----- > From: R-help <[hidden email]> On Behalf Of > [hidden email] > Sent: Tuesday, March 9, 2021 5:06 AM > To: [hidden email] > Subject: [R] Gradient plots in ggplot2 > > I am having trouble with a gradient fill application in ggplot2, caused > by outlier values. In my reprex, most of the values are between 2 and > -2, but there are two outliers, 10 and -15. The outliers stand out well, > which is good, but all the other numbers show almost no colour > variation. I would like to continue with bold colours for the outliers, > while having a more variable gradient, perhaps but not necessarily in a > different colour, for the values between 2 nd -2. Any ideas on how this > can be done? > > > library(ggplot2) > a <- c(rep(1,6),rep(2,6),rep(3,6),rep(4,6)) > b <- c(0.1, 0.5,-0.3, 1.2,-0.4,-1.2, > 0.7, 0.8,-1.2,-0.5,10.0, 0.3, > 0.2,-0.4,-15.,-0.4,-0.9, NA, > 0.1, 1.3,-1.4, 0.5,-0.5, 0.1) > c <- c(rep(c("a","b","c","d","e","f"),4)) > df <- data.frame(a,b) > ggplot(df,aes(x=a,y=c,fill=b))+ > geom_tile()+ > geom_text(label=paste0(round(b,1),"%"), > size=10,colour="gold")+ > scale_fill_gradient(low="red",high="blue",na.value="grey50") > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by Jeff Newmiller
Thank you for your suggestions. I found, after much experimentation,
that scale_fill_gradientn did indeed provide a good solution, as below. library(ggplot2) a <- c(rep(1,6),rep(2,6),rep(3,6),rep(4,6)) b <- c(0.1, 0.5,-0.3, 1.2,-0.4,-1.2, 0.7, 0.8,-1.2,-0.5,10.0, 0.3, 0.2,-0.4,-15.,-0.4,-0.9, NA, 0.1, 1.3,-1.4, 0.5,-0.5, 0.1) c <- c(rep(c("a","b","c","d","e","f"),4)) df <- data.frame(a,b) ggplot(df,aes(x=a,y=c,fill=b))+ geom_tile()+ geom_text(label=ifelse(is.na(b),"NA",paste0(round(b,1),"%")), size=10,colour="black")+ scale_fill_gradientn(colours=c("yellow","green","red","lightblue"), values=c(0.0,0.5,0.7,1.0),na.value="white") Philip On 2021-03-08 23:44, Jeff Newmiller wrote: > Perhaps scale_fill_gradientn() would be useful. > > On March 8, 2021 8:05:52 PM PST, [hidden email] wrote: >> I am having trouble with a gradient fill application in ggplot2, >> caused >> >> by outlier values. In my reprex, most of the values are between 2 and >> -2, but there are two outliers, 10 and -15. The outliers stand out >> well, >> which is good, but all the other numbers show almost no colour >> variation. I would like to continue with bold colours for the >> outliers, >> >> while having a more variable gradient, perhaps but not necessarily in >> a >> >> different colour, for the values between 2 nd -2. Any ideas on how >> this >> >> can be done? >> >> >> library(ggplot2) >> a <- c(rep(1,6),rep(2,6),rep(3,6),rep(4,6)) >> b <- c(0.1, 0.5,-0.3, 1.2,-0.4,-1.2, >> 0.7, 0.8,-1.2,-0.5,10.0, 0.3, >> 0.2,-0.4,-15.,-0.4,-0.9, NA, >> 0.1, 1.3,-1.4, 0.5,-0.5, 0.1) >> c <- c(rep(c("a","b","c","d","e","f"),4)) >> df <- data.frame(a,b) >> ggplot(df,aes(x=a,y=c,fill=b))+ >> geom_tile()+ >> geom_text(label=paste0(round(b,1),"%"), >> size=10,colour="gold")+ >> scale_fill_gradient(low="red",high="blue",na.value="grey50") >> >> ______________________________________________ >> [hidden email] 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. ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |