繁体   English   中英

在 ggplot 中对散点图的图例进行排序并调整该图例的标题

[英]ordering of legend of a scatterplot in ggplot and adjusting the title of that legend

p = ggplot() + 
  geom_line(data = Auckland, aes(x = Date, y = Value, colour = "Auckland"),size=1) +
  geom_line(data = Wellington, aes(x = Date, y = Value, colour = "Wellington"),size =1) +
    geom_line(data = Canterbury, aes(x = Date, y = Value, colour = "Canterbury"),size =1)+
    geom_line(data = Otago, aes(x = Date, y = Value, colour = "Otago"),size =1) +
 xlab("Period") +
  ylab("rent per week (NZD)")+
  labs(title= "Weekly rent in NZ regions over time")
print(p)

在此处输入图像描述

我想知道如何更改我正在考虑的图例列表的顺序

(breaks = c("Auckland", "Wellington", "Canterbury", "Otago"))
#this is the order I want descending

背景信息奥克兰、惠灵顿、坎特伯雷和奥塔哥来自四个不同的数据框,而其他一些具有固定图例顺序的示例仅来自一个数据框。

这行得通吗?

library(tidyverse)
Auckland <- Auckland %>% mutate(region = "Auckland")
Wellington <- Wellington %>% mutate(region = "Wellington")
Canterbury <- Canterbury %>% mutate(region = "Canterbury")
Otago <- Otago %>% mutate(region = "Otago")

bind_rows(Auckland, Wellington, Canterbury, Otago) %>% 
  mutate(region = factor(region, levels = c("Auckland", "Wellington", "Canterbury", "Otago"))) %>% 
  ggplot() +
  geom_line(aes(x = Date, y = Value, colour = region), size = 1) +
  labs(title = "Weekly rent in NZ regions over time", 
       x = "Period", 
       y = "Rent per week (NZD)", 
       colour = "Region")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM