我正在尝试制作一个箱形图,其中X表示检测到某种鸟类属(Vermivora)的3种类型:两种和两种的杂种。在某些情况下,除了“ gwwa”,“ bwwa”和“ hybrid”之外,还没有检测到3个,产生X的第4级,“ none”。
我只想绘制“ gwwa”和“ bwwa”,我希望“ gwwa”为黄色,而“ bwwa”为浅蓝色。
没有第三个框“ NA”就无法生成箱形图,当我尝试省略NA时,仍然有第三个框“ FALSE”。
我在尝试避免这两种结果时收到的重复错误消息是:
错误:美学的长度必须为1或与数据(3)相同:填充
这是我写的:
library(tidyverse)
VERM <- read.csv("C:/1Zack/GWWA Map Stuff/GWWA_bySpecies.csv")
Vermivora <- factor(VERM$Species,levels(VERM$Species)[c(2,1)])
Road_Proximity <- VERM$RoadCount_2km
ggplot(data=VERM, mapping = aes(Vermivora, y=Road_Proximity, fill=Vermivora))+
geom_boxplot()+
scale_fill_manual(values=c("yellow", "lightblue"))
对不起,这是我的第一篇文章,我无法弄清楚如何包含屏幕截图,也无法弄清楚如何将代码的每一行放在自己的行中,而各行之间没有空行。
答案 0 :(得分:0)
我只是在猜测您的数据的样子(因为您没有提供),因此在下面的示例中我组成了一些VERM
data.frame。主要思想是您应该过滤Vermivora
变量(列)中需要的值。
library(tidyverse)
# Some made up data
VERM <- data.frame(Vermivora = c("gwwa", "bwwa", "hybrid", NA),
Road_Proximity = sample(4*10))
我认为这与根据问题描述得到的结果类似:
ggplot(VERM, aes(Vermivora, Road_Proximity, fill = Vermivora)) +
geom_boxplot()
dyplr
的解决方案
过滤变量Vermivora中所需的值。
VERM %>%
filter(Vermivora %in% c("gwwa", "bwwa")) %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))
如果需要在OX轴上确定顺序,请对因子Vermivora进行排序。
VERM %>%
filter(Vermivora %in% c("gwwa", "bwwa")) %>%
mutate(Vermivora = ordered(x = Vermivora,
levels = c("gwwa", "bwwa"))) %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))
data.table
的解决方案
使用data.table
的更简洁的语法:
library(data.table)
library(magrittr) # for piping with %>% (not only dyplr use it;
# pipeline is a Unix trait not a dyplr trait)
setDT(VERM) # converts to data.table from data.frame
VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))
# If you need to order the factor Vermivora:
VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
.[, Vermivora := ordered(x = Vermivora, levels = c("gwwa", "bwwa"))] %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))
请注意,在解决问题时,几乎总是需要查看与您的问题相关的一些数据。您可以通过发布以下行之一的输出来进行尝试:
dput(VERM) # the entire data
dput(head(VERM, 20)) # the first 20 rows
dput(VERM[sample(x = nrow(VERM), size = 20),]) # a sample of 20 rows
# Or make up some data like I did in the above example.