1

I've been trying to change the bars width and I'm using geom_bar (width) but it does not change the bars width, I need to make them more narrow

library(ggplot2)
library(tidyverse)
color_table <- tibble(
  Land_cover = c("A", "B", "C", "D"),
  Color = c("yellow", "darkgreen", "blue4", "maroon3")
)

df <- data.frame(
  name=c("FM_BICEP","FM_NR","FM_TRICEP","FM_H_GRASP1","FM_CS_SPE","FM_MOS_SFL","FM_H_GRASP3*","FM_FS_RET","FM_W_SE3","FM_FS_ABD*","FM_MOS_SAB") ,  
  value=c(1.7,1.8,1.8,22.0,26.8,27.4,27.9,31.8,33.4,35.8,35.8),
  group=c("A","A","A","C","D","A","C","A","B","A","A")
)

df$name <- factor(df$name, levels = df$name)
df$group <- factor(df$group, levels = color_table$Land_cover)

# Barplot
ggplot(df, aes(x=name, y=value,fill = group)) +
  geom_bar(stat = "identity", aes(fill=group))+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
   scale_fill_manual(values = color_table$Color) + geom_col(width = 0.4)
1
  • Remove geom_col(width = 0.4) (except you have any reason to add a duplicated col layer) and move width = 0.4 to the geom_bar Commented Aug 17, 2022 at 9:42

1 Answer 1

2

I recommend you to use geom_col() as you use both x and y axis. It's the same as geom_bar(scale = "identity").

Also, to width work, I moved it to inside the geom_bar() function.

library(ggplot2)
library(tidyverse)
color_table <- tibble(
  Land_cover = c("A", "B", "C", "D"),
  Color = c("yellow", "darkgreen", "blue4", "maroon3")
)

df <- data.frame(
  name=c("FM_BICEP","FM_NR","FM_TRICEP","FM_H_GRASP1","FM_CS_SPE","FM_MOS_SFL","FM_H_GRASP3*","FM_FS_RET","FM_W_SE3","FM_FS_ABD*","FM_MOS_SAB") ,  
  value=c(1.7,1.8,1.8,22.0,26.8,27.4,27.9,31.8,33.4,35.8,35.8),
  group=c("A","A","A","C","D","A","C","A","B","A","A")
)

df$name <- factor(df$name, levels = df$name)
df$group <- factor(df$group, levels = color_table$Land_cover)

# Barplot
ggplot(df, aes(x=name, y=value,fill = group)) +
  geom_col(aes(fill=group),
           width = 0.4)+
  scale_fill_manual(values = color_table$Color)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Created on 2022-11-19 with reprex v2.0.2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.