Home > Mobile >  Having issues to plot with ggplot2
Having issues to plot with ggplot2

Time:12-30

Could someone help me with a hint to plot these vervet monkey behavioral data? Here are the codes and my expectation.

I want to plot vervet monkeys and I got stuck.

males<-c(21,50,25,12,15,1,5)
females<-c(20,30,22,12,16,46,8)
activities<- c("playing", "grooming", "dancing", "feeding", "mating", "fighting", "eating")

Vervet<-data.frame(activities,females, males)

Vervet |> 
  pivot_longer(females:males, values_to = "Count", names_to = "Gender") |>
  ggplot(mapping = aes(x= activities , y= Count, fill= Gender))  geom_col(position = "dodge") 
  scale_fill_continuous() 
  coord_flip() 
theme_bw() 
theme(panel.grid = element_blank())

And here are my expectation graph.

enter image description here

CodePudding user response:

I have created more or less what you were looking for. The colors don't match exactly, but you can change that for sure. Maybe also reconsider having the axis labels in place.

library(tidyverse)
males<-c(21,50,25,12,15,1,5)
females<-c(20,30,22,12,16,46,8)
activities<- c("playing", "grooming", "dancing", "feeding", "mating", "fighting", "eating")

Vervet<-data.frame(activities,females, males)

Vervet %>%
  pivot_longer(females:males, values_to = "Count", names_to = "Gender") |>
  ggplot(aes(x= activities , y= Count, fill= Gender, color= Gender))  
  geom_col(position = "dodge") 
  scale_color_manual(values = c("lightblue", "khaki")) 
  scale_fill_manual(values = alpha(c("lightblue", "khaki"), 0.5)) 
  theme_bw() 
  theme(panel.grid = element_blank(),
        panel.border = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.line =  element_line(arrow=arrow(type = "closed", 
                                              length= unit(unit(3, "mm"))))) 
  labs(x= "Activities")

Created on 2022-12-29 with enter image description here

  • Related