[R] Colour for unique label with ggplot

phii m@iii@g oii phiiipsmith@c@ phii m@iii@g oii phiiipsmith@c@
Thu Jun 20 13:24:34 CEST 2019


Thank you so much Eric. I had tried searching stackoverflow too, but I 
could not find the right way to word the query.

Philip

On 2019-06-20 04:03, Eric Berger wrote:
> Hi Phil,
> 
> Try this
> 
> library(tidyverse)
> # Test data
> dta <- data.frame(dta_names=c("Series 1","Series 2","Series 3","Series
> 4"), dta_values=c(0.5,1.3,-0.7,0.2))
> dta$dta_names <- as.character(dta$dta_names) # convert from factor
> 
> DTA$LABELCOLOR <- C("BLACK","RED","BLACK","BLACK")
> # Bar chart
> ggplot(dta,aes(x=dta_names,y=dta_values)) +
>   geom_bar(stat="identity",fill="darkblue") +
>   geom_bar(data=dta[2, ],stat="identity",aes(x=dta_names,
> y=dta_values),
>            fill="red") +
>   THEME(AXIS.TEXT.Y = ELEMENT_TEXT(COLOUR=DTA$LABELCOLOR)) +
>   labs(title = "Example bar chart") +
>   coord_flip()
> 
> I did not know how to do this. I did a search on
> 
> ggplot format axis labels categorical data different each category
> 
> and the first result was the following URL which provided the
> necessary information
> 
> https://stackoverflow.com/questions/38862303/customize-ggplot2-axis-labels-with-different-colors
> 
> 
> HTH,
> Eric
> 
> On Thu, Jun 20, 2019 at 4:24 AM <phil using philipsmith.ca> wrote:
> 
>> # RStudio version 1.1.463
>> # sessionInfo()
>> # R version 3.5.1 (2018-07-02)
>> # Platform: x86_64-apple-darwin15.6.0 (64-bit)
>> # Running under: macOS  10.14.5
>> 
>> # I am constructing a bar chart using ggplot, as in the example
>> below.
>> # I want to highlight "Series 2" by colouring the bar red,
>> # with the other bars coloured blue. That works.
>> # I also want to colour the label "Series 2" (only) in red, but I
>> have
>> not
>> # been able to find a way to do that.
>> # Here is the example:
>> 
>> library(tidyverse)
>> # Test data
>> dta <- data.frame(dta_names=c("Series 1","Series 2","Series
>> 3","Series
>> 4"),dta_values=c(0.5,1.3,-0.7,0.2))
>> dta$dta_names <- as.character(dta$dta_names) # convert from factor
>> # Bar chart
>> ggplot(dta,aes(x=dta_names,y=dta_values)) +
>> geom_bar(stat="identity",fill="darkblue") +
>> geom_bar(data=dta[2, ],stat="identity",aes(x=dta_names,
>> y=dta_values),
>> fill="red") +
>> labs(title = "Example bar chart") +
>> coord_flip()
>> 
>> # Any suggestions appreciated.
>> 
>> ______________________________________________
>> R-help using 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.



More information about the R-help mailing list