Add line break into the x-axis

I am trying to add break into the text "Extremely difficult". If someone know, please help me.
Thanks
Here is code:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
CATA <- read.table(header = T, text = 'hedonics age Percentage
5 45-54 0
5 55-64 2.1
5 65-74 0
5 75 0
6 45-54 0
6 55-64 0
6 65-74 0
6 75 0
7 45-54 6.25
7 55-64 4.17
7 65-74 10.42
7 75 6.25
8 45-54 0
8 55-64 0
8 65-74 0
8 75 0
9 45-54 0
9 55-64 0
9 65-74 0
9 75 0')
ggplot(CATA, aes(factor(hedonics), Percentage, fill=age)) + 
  theme(axis.title.x = element_blank()) +
  geom_bar(stat="identity", position = "dodge", width = 1) +  
  scale_fill_grey(start = 0.8, end = 0.2) + 
  theme(legend.title = element_blank()) + theme(legend.position = c(.80, .80)) +
  theme(axis.text.x = element_text(face="bold", size=7), 
        axis.text.y = element_text(face="bold", size=10),
        axis.title.y = element_text(size=10, face="bold")) +
  scale_x_discrete(labels=c("5", "", "","", "Extremely difficult"))

Created on 2020-05-19 by the reprex package (v0.3.0)

scale_x_discrete(labels=c("5", "", "","", "Extremely\ndifficult"))

works for me.

2 Likes

Thanks for the information, I was so close but still so far :grinning:
@HanOostdijk Do you know how I can change color from grey to blue, something like this color (attached picture)

Code for the plot:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
OEQ <- read.table(header = T, text = 'hedonics age Percentage
                   5 45-54 0
                   5 55-64 0
                   5 65-74 2.1
                   5 75 2.1
                   6 45-54 2.1
                   6 55-64 0
                   6 65-74 6.3
                   6 75 0
                   7 45-54 6.3
                   7 55-64 14.6
                   7 65-74 2.1
                   7 75 2.1
                   8 45-54 12.6
                   8 55-64 6.3
                   8 65-74 2.1
                   8 75 2.1
                   9 45-54 6.3
                   9 55-64 2.1
                   9 65-74 2.1
                   9 75 4.2')
ggplot(OEQ, aes(factor(hedonics), Percentage, fill=age)) + 
  theme(axis.title.x = element_blank()) +
  geom_bar(stat="identity", position = "dodge", width = 1) +  
  scale_color_gradient2(low = "red",high = "white") + 
  theme(legend.title = element_blank()) + theme(legend.position = c(.80, .80)) +
  theme(axis.text.x = element_text(face="bold", size=7), 
        axis.text.y = element_text(face="bold", size=10),
        axis.title.y = element_text(size=10, face="bold")) +
  scale_x_discrete(labels=c("5", "", "","", "Extremely\ndifficult"))

Created on 2020-05-20 by the reprex package (v0.3.0)
Annotation 2020-05-20 111206

I understand that you want to change the background grey into a dark blue?
I seldom use ggplot2 so I had to google how to do that: searching for 'ggplot2 background'.
One of the first results suggested the theme option panel.background.
I inserted that in your code and removed the scale_color_gradient2 that plays no role here (?)

It possible to specify exactly the color you need by using rgb codes. See e.g. here. (Also found this page by googling).

ggplot(OEQ, aes(factor(hedonics), Percentage, fill=age)) + 
  theme(axis.title.x = element_blank()) +
  geom_bar(stat="identity", position = "dodge", width = 1) +  
  # scale_color_gradient2(low = "red",high = "white") +  # removed
  theme(legend.title = element_blank()) + theme(legend.position = c(.80, .80)) +
  theme(axis.text.x = element_text(face="bold", size=7), 
        axis.text.y = element_text(face="bold", size=10),
        axis.title.y = element_text(size=10, face="bold"),
        panel.background = element_rect(fill = "darkblue") #inserted
        ) +
  scale_x_discrete(labels=c("5", "", "","", "Extremely\ndifficult"))

Thanks, actually I was intended to change bar colors from grey to blue, but not background. Sorry for not being clearer.

Again googled and found ggplot2-barplots-quick-start-guide-r-software-and-data-visualization. From this (of course you have to adapt the colors) I added to your code:

  scale_x_discrete(labels=c("5", "", "","", "Extremely\ndifficult")) +
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9","#123456"))

A solution with Color Palettes could be done by using package RColorBrewer and adding (again choose an appropriate palet) :

  .......... + 
scale_fill_brewer(palette="Dark2")
1 Like

Thanks buddy @HanOostdijk. I think I should also google more to make it perfect.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.