我是R的新手,但遇到了问题。
我使用geom_point
作了一个散点图。
我设法重命名构面网格标签,但是我不知道如何重命名图形图例。我尝试将行geom_point(aes(colour = Diet))
重命名为geom_point(aes(colour = diet_names))
,但是发生以下错误:
Error: Aesthetics must be either length 1 or the same as the data (23): colour.
如果我删除以下所有值之一(例如,只留下fish_or =“ Fish”),则不会发生该错误,但是显然我不希望表上只有一个彩色值。
diet_names <- c(
fish_or = "Fish" ,
fruit_pu = "Fruit" ,
insects_nectar_bl = "Insects & Nectar" ,
insects_re = "Insects" ,
seeds_gr = "Seeds")
我想将图例编辑为更清晰的内容,因为它是我制作的海报的数据。谢谢:)
完整代码:
library(ggplot2)
library(dplyr)
Birdbeaks <- read.csv("Birdbeaks.csv", stringsAsFactors = FALSE)
diet_names <- c(
fish_or = "Fish" ,
fruit_pu = "Fruit" ,
insects_nectar_bl = "Insects & Nectar" ,
insects_re = "Insects" ,
seeds_gr = "Seeds")
ggplot(Birdbeaks, aes(bill_lengthavg, bill_depthavg)) +
geom_point(aes(colour = diet)) +
labs(x = "Bill Length(cm)", y = "Bill Depth(cm)", colour = "Diet")+
guides(col = guide_legend(override.aes = list(shape = 15, size = 5))) +
theme_update()+
facet_grid(~diet, scales="fixed", labeller = as_labeller(diet_names))
答案 0 :(得分:1)
希望这很有用
library(magrittr)
library(dplyr)
library(ggplot2)
Birdbeaks %>%
mutate(diet = case_when(
diet == "fish_or" ~ "Fish",
diet == "fruit_pu" ~ "Fruit",
diet == "insects_nectar_bl" ~ "Insects & Nectar",
diet == "insects_re" ~ "Insects",
diet == "seeds_gr" ~ "Seeds"
)) %>%
ggplot(aes(bill_lengthavg, bill_depthavg)) +
geom_point(aes(colour = diet)) +
labs(x = "Bill Length(cm)", y = "Bill Depth(cm)", colour = "Diet") +
guides(col = guide_legend(override.aes = list(shape = 15, size = 5))) +
theme_update() +
facet_grid(~diet, scales = "fixed")