0

I am a beginner to R so I apologise in advance if this is a very basic question.

I am plotting a box plot showing the type of bars and their various quantities in 1990 and 2010. I have two problems here:

  1. I wish to remove the years of 1980, 2000, 2020. I tried using scale_x_discrete("1990","2010") but it doesn't seem to work.
  2. The legend at the bottom lists the names of the bars. However, i wish to replace the . with spaces. For example, changing mid.channel.bar to Mid-channel bar.

 

library(ggplot2)
library(tidyr)
library(reshape2)

barCount <- tibble::tribble(
             ~Year, ~Lateral.bar, ~Bar.accreted.to.island, ~Mid.channel.bar,
              1990,          105,                     134,               62,
              2010,          102,                     189,              102
             )

df2 <- melt(barCount, id="Year")
barPlot <- ggplot(df2, aes(Year,value)) +
  geom_bar(aes(fill=variable),position="dodge",stat="identity") +
  labs(y="Quantity",fill="")+
  scale_fill_manual("Legend",values=c("Lateral.bar"="khaki4","Bar.accreted.to.island"="Khaki2",
                    "Mid.channel.bar"="ivory"))

#modifying axis 
barPlot <- barPlot + theme(
  axis.title.x = element_blank(),
  axis.title.y = element_text(size=14),
  axis.text.x = element_text(size=14),
  legend.position="bottom"
  )

barPlot
jay.sf
  • 60,139
  • 8
  • 53
  • 110
catlovingtaco
  • 123
  • 10
  • Hi Jay, thank you for taking time out to look into my question. I tried your method but then the entire bar plot just didn't show up /: – catlovingtaco Mar 20 '19 at 09:11
  • Your second question has already an answer on Stack Overflow: https://stackoverflow.com/q/31518150/6574038. BTW, [multiple questions are discouraged](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts), split them. – jay.sf Mar 20 '19 at 09:12
  • 1
    I think what the op means is that they show up on the scale though, because the variable is numeric in his df @jay.sf. also you need to load reshape2 in the example for melt() – Benjamin Schwetz Mar 20 '19 at 09:19
  • @BerndKonfuzius Yes, you're right. – jay.sf Mar 20 '19 at 09:32

1 Answers1

2

If you treat the year column as a factor, ggplot will give you the plot you want. Plus str_replace_all on the variable column will swap the points for spaces:

library(reshape2)
library(tidyverse)
barCount <- tibble::tribble(
~Year, ~Lateral.bar, ~Bar.accreted.to.island, ~Mid.channel.bar,
1990,          105,                     134,               62,
2010,          102,                     189,              102
)


df2 <- melt(barCount, id="Year") %>% 
  mutate(
    Year = Year %>% as.factor(),
    variable = variable %>% str_replace_all("\\.", " ")
  )
barPlot <- ggplot(df2, aes(Year,value)) +
geom_bar(aes(fill=variable),position="dodge",stat="identity") +
labs(y="Quantity",fill="")+
scale_fill_manual("Legend",values=c("Lateral bar"="khaki4","Bar accreted to island"="Khaki2","Mid channel bar"="ivory"))

#modifying axis 
barPlot <- barPlot + theme(
axis.title.x = element_blank(),
axis.title.y = element_text(size=14),
axis.text.x = element_text(size=14),
legend.position="bottom"

)

barPlot
Benjamin Schwetz
  • 624
  • 5
  • 17
  • Thank you for taking time out to answer my question. I have one more question on top of this. I have tried to put labels on top of the geom_bar, but the labels for each of the 3 classes are vertically stacked. I have tried using: geom_text(aes(label=value),position=position_dodge(width=0.9),vjust=-1), varying the numbers to no avail. Would you happen to have a suggestion? Thank you again. – catlovingtaco Mar 20 '19 at 09:35
  • Not sure what you mean. I would suggest reading the cookbook on ggplot http://www.cookbook-r.com/Graphs/ and ones you are familiar with the basics of ggplot, check out the cowplot package,ggforce and the like – Benjamin Schwetz Mar 20 '19 at 13:27