-1

I am trying to make the y axis of the second plot over 2 lines. Using '\n' for the first plot worked fine but using it on the second makes the text in odd places (maybe because of the italics).

p1 <- ggplot(data = new_data) + 
  geom_line(mapping = aes(x = Date, 
                          y = Proportion, 
                          group = Species, 
                          colour = Species)) +
  scale_colour_manual(values=c(Golden_Trevally="goldenrod2", 
                               Red_Snapper="firebrick2", 
                               Sharksucker_Remora="darkolivegreen3", 
                               Juvenile_Remora="aquamarine2")) + 
  xlab("Date (2014-2018)") + 
  ylab("Total Presence \n Per Month ") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
  theme(legend.position="top") + 
  labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) + 
  geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') + 
  xlab("Date (2014-2018)") + 
  ylab("Total Number of "~italic(\nM.alfredi)~" Encounters") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) 
grid.arrange(p1,p2)

see plot

Ben Norris
  • 5,226
  • 2
  • 7
  • 14
  • Please provide a reproducible example. – daniellga Aug 16 '20 at 15:55
  • In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Aug 16 '20 at 16:16

2 Answers2

2

You can try this:

  geom_line(mapping = aes(x = Date, 
                          y = Proportion, 
                          group = Species, 
                          colour = Species)) +
  scale_colour_manual(values=c(Golden_Trevally="goldenrod2", 
                               Red_Snapper="firebrick2", 
                               Sharksucker_Remora="darkolivegreen3", 
                               Juvenile_Remora="aquamarine2")) + 
  xlab("Date (2014-2018)") + 
  ylab("Total Presence \n Per Month ") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
  theme(legend.position="top") + 
  labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) + 
  geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') + 
  labs(x="Date (2014-2018)",
       y=expression(atop(paste("Total Number of"), paste(italic("M.alfredi"), " Encounters")))) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) 
grid.arrange(p1,p2)
P.Chakytei
  • 412
  • 2
  • 12
-1

You need space between \n and M.alfredi in p2. Since there is no reproducible example, here is my suggestion for the second plot,

  p2 <- ggplot(data = new_data_counts) + 
  geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') + 
  xlab("Date (2014-2018)") + 
  ylab("Total Number of "~italic(\n M.alfredi)~" Encounters") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) 

It should solve your problem.

ssaha
  • 441
  • 2
  • 10