R语言 使用ggplot2绘图时遇到问题

vxqlmq5t  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(289)

有没有人能帮我一个提示,以绘制这些黑长尾猴的行为数据?以下是代码和我的期望。
我想画黑长尾猴但我被困住了。

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())

这是我的期望图。

suzh9iv8

suzh9iv81#

我已经创建了或多或少你所寻找的。颜色不完全匹配,但你可以改变这一点是肯定的。也许还重新考虑有轴标签的地方。

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")

创建于2022年12月29日,使用reprex v2.0.2

kmpatx3s

kmpatx3s2#

只是调整了颜色。主题部分来自@Nick Glättli:

library(tidyr)
library(ggplot2)

Vervet |> 
  pivot_longer(-activities, values_to = "Count", names_to = "Gender") |>
  ggplot(mapping = aes(x= activities , y= Count, fill= Gender, color=Gender))+
  geom_col(position = "dodge")+
  scale_fill_manual(values = c("#d9e8fb", "#ffe6cd"))+
  scale_color_manual(values = c("#899cba", "#c49f49"))+
  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")))))

相关问题