 # group bar plot x-axis help

I am having problem to set x-axis, if someone can please let me know how I can set my x-axis from one to nine in ascending order. Here is code:

``````library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
# Create a dataset
one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
geom_bar(stat="identity", position = "dodge") +  scale_fill_brewer(palette = "Set1")
`````` Created on 2020-05-18 by the reprex package (v0.3.0)

The default behavior of R is to set factor levels in alphabetical order. If you want a different order, you can set the levels with the factor() function.

``````library(ggplot2)

one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
trend\$hedonic <- factor(trend\$hedonic, levels = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"))
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
geom_bar(stat="identity", position = "dodge") +  scale_fill_brewer(palette = "Set1")
`````` Created on 2020-05-17 by the reprex package (v0.3.0)

1 Like

Thanks for the information and help. I was trying to remove x-axis text which I did successfully. Can I also customize it to have one two categories, such as "Not at all difficult" at position one and "extremely difficult" at position nine. I tried this code, trend + scale_x_discrete(breaks=c("0.5","1","2"),
labels=c("Not at all difficult", "two", "three",
"four", "five", "six", "seven", "eight",
"Extremely difficult")) but its not working.

``````library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
# Create a dataset
one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
trend\$hedonic <- factor(trend\$hedonic, levels = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"))
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
theme(axis.title.x = element_blank(), axis.text.x = element_blank()) +
geom_bar(stat="identity", position = "dodge") +  scale_fill_brewer(palette = "Set1")
`````` Created on 2020-05-19 by the reprex package (v0.3.0)

I removed the axis.text.x = element_blank() from the theme.

``````library(ggplot2)

one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
trend\$hedonic <- factor(trend\$hedonic, levels = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"))
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
theme(axis.title.x = element_blank()) +
geom_bar(stat="identity", position = "dodge") +  scale_fill_brewer(palette = "Set1") +
scale_x_discrete(labels=c("Not at all difficult", "two", "three",
"four", "five", "six", "seven", "eight",
"Extremely difficult"))
`````` Created on 2020-05-18 by the reprex package (v0.2.1)

1 Like

Thanks, how I can remove 2nd to 8th position axis text. Just want to keep 1st and last axis text.

@FJCC Can I increase space between grouped bars. I want it as overlapping bars but at the same time I was wondering if somehow I can increase space between groupings because they are not looking as much separate as I want to. Also, if I can reduce margin of legend box and I need just 2 anchors "not at all difficult" at the first position and "extremely difficult" at the last position. Here is code:

``````library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
# Create a dataset
one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
trend\$hedonic <- factor(trend\$hedonic, levels = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"))
tiff(file = "difficultyscale.tiff", width = 1800, height = 1800, pointsize = 10, units = "px", res = 600)
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(size=10, face="bold")) +
geom_bar(stat="identity", position = "dodge") +  scale_fill_manual(values = c("grey", "blue")) +
theme(legend.title = element_blank()) + theme(legend.position = c(.20, .80)) +
theme(axis.text.x = element_text(face="bold", size=10),
axis.text.y = element_text(face="bold", size=10)) +
scale_x_discrete(labels=c("Not at all difficult", "two", "three",
"four", "five", "six", "seven", "eight",
"Extremely difficult")) +
geom_bar(stat = "identity", position = position_dodge(width = 0.5))
dev.off()
#> png
#>   2
``````

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

Is this closer to what you want. I decreased the width of the bars so that there is more room between each group.

``````library(ggplot2)

one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
trend\$hedonic <- factor(trend\$hedonic, levels = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"))
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
theme(axis.title.x = element_blank()) +
geom_bar(stat="identity", position = "dodge", width = 0.5) +  scale_fill_brewer(palette = "Set1") +
scale_x_discrete(labels=c("Not at all difficult", "", "",
"", "", "", "", "",
"Extremely difficult"))
`````` Created on 2020-05-18 by the reprex package (v0.3.0)

1 Like

Thanks @FJCC. I got it. When I created tiff file of this graph. X-axis text is chopping in b/w. Do you know how I can prevent this. For image I am using this code:
tiff(file = "difficultyscale.tiff", width = 2000, height = 2000, pointsize = 10, units = "px", res = 600)
dev.off()
Here is code:

``````library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
# Create a dataset
one CATA 31
one OEQ 6
two CATA 0
two OEQ 0
three CATA 17
three OEQ 10
four CATA 13
four OEQ 0
five CATA 2
five OEQ 4
six CATA 6
six OEQ 13
seven CATA 31
seven OEQ 25
eight CATA 0
eight OEQ 25
nine CATA 0
nine OEQ 17')
trend\$hedonic <- factor(trend\$hedonic, levels = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"))
ggplot(trend, aes(factor(hedonic), Percentage, fill=Category)) +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(size=10, face="bold")) +
geom_bar(stat="identity", position = "dodge", width = 0.5) +  scale_fill_manual(values = c("grey", "blue")) +
theme(legend.title = element_blank()) + theme(legend.position = c(.30, .80)) +
theme(axis.text.x = element_text(face="bold", size=6),
axis.text.y = element_text(face="bold", size=10),
legend.margin = margin(t = 0, unit='pt'),
legend.box.margin = margin(0.4,0.4,0.4,0.4,"cm")) +
scale_x_discrete(labels=c("Not at all difficult", "", "",
"", "", "", "", "",
"Extremely difficult")) +
geom_bar(stat = "identity", position = position_dodge(width = 0.5))
`````` Created on 2020-05-19 by the reprex package (v0.3.0)

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