Samrat
Samrat

Reputation: 89

How to change the position of a single bar in Bar chart?

I have made a barchart for 12 counties with three factors. Except some factors (WWBP, WWBF) does not apply to some counties so they are empty. I used position_dodge() to make the width consistent.

The problem i am having is that when i do position_dodge(preserve = single) the places with one bar shows in a wrong position. In other words, for those with missing factors, i would like to show the bar either exactly in the middle or in the same position it would have taken if there were three factor.

For example (see image): For Adams county, i would like to position the bar exactly in the middle or on the right side so it is consistent.

Following is my code. I would appreciate the help.

Thanks.[![Erosion by factor][1]][1]

library(ggplot2)
library(tidyverse)
mydata2<-structure(list(Rotation = c("WW-B-F", "WW-B-P", "WW-F", "WW-B-F", 
                            "WW-B-P", "WW-F", "WW-B-F", "WW-B-P", "WW-F", "WW-B-F", "WW-B-P", 
                            "WW-F", "WW-B-F", "WW-B-P", "WW-F", "WW-B-F", "WW-B-P", "WW-B-F", 
                            "WW-F", "WW-F", "WW-F", "WW-F", "WW-F", "WW-F"),
                        avg = c(6,  1, 3, 11, 18,  20, 14, 28, 20,  10, 7.6, 1.95, 15, 11, 1, 7, 5,  6, 12, 2, 16, 11, 4, 20), County = c("Asotin", 
        "Asotin", "Asotin", "Garfield", "Garfield", "Garfield", "Columbia", 
 "Columbia", "Columbia", "Walla Walla", "Walla Walla", "Walla Walla", 
  "Whitman", "Whitman", "Whitman", "Spokane", "Spokane", "Lincoln", 
 "Lincoln", "Franklin", "Adams", "Benton", "Grant", "Douglas")), class = "data.frame", row.names = c(NA,-24L))                                                                                                                                                                                                                  

rot_grphs_mat <- mydata2
k4="Rotation"
p4<-ggplot(rot_grphs_mat, aes(fill=rot_grphs_mat[,paste(k4)], y=`avg`, x=County))+
 scale_y_continuous(limits=c(0,60), n.breaks =10)+
  scale_fill_manual(paste(k4),values = c(`WW-B-P`="#009982",`WW-F`="#6dbc6a", `WW-B-F`="#004766"))+
  geom_bar(position = position_dodge(preserve = 'single'), stat = "identity" )+
  labs(title = paste(k4))+
  labs(y = expression(paste("Average Erosion t  ", ha^-1, yr^-1)))+
  theme_bw() +scale_color_discrete(name=paste(k4))+
  theme(plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"),
        legend.position = c(.98, .98),
        legend.direction = "horizontal",
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.margin = margin(6, 6, 6, 6), 
        legend.title = element_blank(),
        legend.text = element_text(size = 12)
  )+
  theme(axis.title.x = element_blank(),axis.text.x.bottom = element_text(angle = 70, hjust = 1, size = 15),plot.margin = unit(c(0.3,0.3,0.3,0.3), "cm"),plot.title = element_text(hjust = 0.5), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

p4 ### final result ```


  [1]: https://i.sstatic.net/hk2I2.png

Upvotes: 0

Views: 242

Answers (1)

Jon Spring
Jon Spring

Reputation: 66425

p4<-ggplot(rot_grphs_mat %>% tidyr::complete(County, Rotation), 
           aes(fill=Rotation, y=`avg`, x=County))+
# ... rest of code

enter image description here

Upvotes: 1

Related Questions