ggplot2: less than equal sign

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot2: less than equal sign

soon yi
Hi

I am trying to add a less than equal sign to a plot. I have previously done this using unicode but is not working in this instance. Any suggestions would be great

thanks

example code:

library(ggplot2)
df<-data.frame(vis=c(0,0,1,1) , count=c(10,15,20,10) , grp=c(0,1,0,1))
df$grp <-factor(df$grp ,levels=c(0,1) , labels =c("x \u2265 10" , "x > 10"))
ggplot(df, aes(x = factor(vis), y=count , fill=grp))  + geom_bar(stat = "identity")
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2: less than equal sign

Ista Zahn
Hi,

If you want to stick with the unicode way make sure to set your
encoding system, e.g., on Mac

Sys.setlocale("LC_ALL", "en_US.UTF-8")

This differs on Windows/Linux so see ?Sys.setlocale for examples on
other platforms.

Best,
Ista

On Thu, Mar 28, 2013 at 5:22 PM, soon yi <[hidden email]> wrote:

> Hi
>
> I am trying to add a less than equal sign to a plot. I have previously done
> this using unicode but is not working in this instance. Any suggestions
> would be great
>
> thanks
>
> example code:
>
> library(ggplot2)
> df<-data.frame(vis=c(0,0,1,1) , count=c(10,15,20,10) , grp=c(0,1,0,1))
> df$grp <-factor(df$grp ,levels=c(0,1) , labels =c("x \u2265 10" , "x > 10"))
> ggplot(df, aes(x = factor(vis), y=count , fill=grp))  + geom_bar(stat =
> "identity")
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/ggplot2-less-than-equal-sign-tp4662784.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2: less than equal sign

djmuseR
In reply to this post by soon yi
Hi:

Here's a way you could do it entirely within ggplot2. The annotation
functions have a parse = argument which allows you to pass character
string representations of math expressions, but there is no such thing
in the scale functions, so you need a different approach.

library(ggplot2)
df <- data.frame(vis=c(0,0,1,1) , count=c(10,15,20,10) , grp=c(0,1,0,1))

# Generate a list of expressions that will become the legend labels
lbs <- list(expression(x <= 10), expression(x > 10))

ggplot(df, aes(x = factor(vis), y=count , fill=factor(grp)))  +
   geom_bar(stat ="identity") +
   scale_fill_manual(breaks = levels(factor(df$grp)),
                     values = c("blue", "orange"),
                     labels = lbs)

The specifications in scale_fill_manual() are:
    - breaks: the values to go on the horizontal axis
    - values: the vector of fill colors for each level of grp
    - labels: the legend labels
Notice that the labels = takes a list of expressions as its argument.
This approach gives you more control over the legend and choice of
fill colors at the expense of a couple of lines of code. To change the
axis and legend titles, one can use the labs() function; e.g.,

last_plot() + labs(x = "Visibility", y = "Frequency", fill = "Threshold")

Dennis

On Thu, Mar 28, 2013 at 2:22 PM, soon yi <[hidden email]> wrote:

> Hi
>
> I am trying to add a less than equal sign to a plot. I have previously done
> this using unicode but is not working in this instance. Any suggestions
> would be great
>
> thanks
>
> example code:
>
> library(ggplot2)
> df<-data.frame(vis=c(0,0,1,1) , count=c(10,15,20,10) , grp=c(0,1,0,1))
> df$grp <-factor(df$grp ,levels=c(0,1) , labels =c("x \u2265 10" , "x > 10"))
> ggplot(df, aes(x = factor(vis), y=count , fill=grp))  + geom_bar(stat =
> "identity")
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/ggplot2-less-than-equal-sign-tp4662784.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2: less than equal sign

soon yi
Thank you both for the suggestions.

That worked great djmuse.

djmuseR wrote
Hi:

Here's a way you could do it entirely within ggplot2. The annotation
functions have a parse = argument which allows you to pass character
string representations of math expressions, but there is no such thing
in the scale functions, so you need a different approach.

library(ggplot2)
df <- data.frame(vis=c(0,0,1,1) , count=c(10,15,20,10) , grp=c(0,1,0,1))

# Generate a list of expressions that will become the legend labels
lbs <- list(expression(x <= 10), expression(x > 10))

ggplot(df, aes(x = factor(vis), y=count , fill=factor(grp)))  +
   geom_bar(stat ="identity") +
   scale_fill_manual(breaks = levels(factor(df$grp)),
                     values = c("blue", "orange"),
                     labels = lbs)

The specifications in scale_fill_manual() are:
    - breaks: the values to go on the horizontal axis
    - values: the vector of fill colors for each level of grp
    - labels: the legend labels
Notice that the labels = takes a list of expressions as its argument.
This approach gives you more control over the legend and choice of
fill colors at the expense of a couple of lines of code. To change the
axis and legend titles, one can use the labs() function; e.g.,

last_plot() + labs(x = "Visibility", y = "Frequency", fill = "Threshold")

Dennis

On Thu, Mar 28, 2013 at 2:22 PM, soon yi <[hidden email]> wrote:
> Hi
>
> I am trying to add a less than equal sign to a plot. I have previously done
> this using unicode but is not working in this instance. Any suggestions
> would be great
>
> thanks
>
> example code:
>
> library(ggplot2)
> df<-data.frame(vis=c(0,0,1,1) , count=c(10,15,20,10) , grp=c(0,1,0,1))
> df$grp <-factor(df$grp ,levels=c(0,1) , labels =c("x \u2265 10" , "x > 10"))
> ggplot(df, aes(x = factor(vis), y=count , fill=grp))  + geom_bar(stat =
> "identity")
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/ggplot2-less-than-equal-sign-tp4662784.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.