如何从此图例ggplot中删除科学计数法

时间:2019-10-19 12:41:02

标签: r ggplot2 scientific-notation

我从中使用代码来创建图例范围: R ggplot2 - geom_point custom color ranges and colors

break_year_map_data_nona的输出的一小部分样本(不幸的是,上传的文件停止了工作,因此添加了它,我想总比没有好)

structure(list(value = c(1991, 1991, 1991, 1991, 1991, 1991, 
1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 
1991, 1991, 1991), iso2c = c("AO", "AO", "AO", "AO", "AO", "AO", 
"AO", "AO", "AO", "AO", "AO", "AO", "AO", "AO", "AO", "AO", "AO", 
"AO", "AO", "AO"), mapname = c("Angola", "Angola", "Angola", 
"Angola", "Angola", "Angola", "Angola", "Angola", "Angola", "Angola", 
"Angola", "Angola", "Angola", "Angola", "Angola", "Angola", "Angola", 
"Angola", "Angola", "Angola"), long = c(23.9665031433105, 23.98828125, 
24.0100574493408, 24.0255870819092, 24.0414066314697, 24.0466804504395, 
24.029296875, 24.0146484375, 23.98681640625, 23.9709968566895, 
23.98388671875, 23.9734382629395, 23.9623050689697, 23.9588871002197, 
23.9964847564697, 23.9913101196289, 23.9447269439697, 23.9093761444092, 
23.8865242004395, 23.8824214935303), lat = c(-10.8717775344849, 
-11.0028324127197, -11.1847658157349, -11.3156251907349, -11.3741207122803, 
-11.4053707122803, -11.4391593933105, -11.5176753997803, -11.5872068405151, 
-11.6358404159546, -11.7249994277954, -11.8529300689697, -11.9878902435303, 
-12.1177730560303, -12.3506832122803, -12.4221677780151, -12.5437498092651, 
-12.6361322402954, -12.7432613372803, -12.7990236282349), group = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), order = 423:442, 
    subregion = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_)), row.names = c(NA, 20L), class = "data.frame")

这是我正在使用的代码

break_year_map_data_nona %>% 
    ggplot(aes(long, lat, group = group, fill = cut(value, c(1961, 1980, 1990, 2000, 2010, 2016)))) +
    geom_polygon() +
    coord_map(xlim=c(-180,180)) +
    theme_void() +
    labs(title = "", fill = "Break year")

但是传说得到了科学的记号:

Map with scientific notation

我试图按照这里的提示进行操作 How to change scientific notation on legend labels in ggplot2

由于我有离散数据,因此尝试了以下代码:

break_year_map_data_nona %>% 
    ggplot(aes(long, lat, group = group, fill = cut(value, c(1961, 1980, 1990, 2000, 2010, 2016)))) +
    geom_polygon() +
    scale_fill_discrete(labels = comma) +
    coord_map(xlim=c(-180,180)) +
    theme_void() +
    labs(title = "", fill = "Break year")

但是我收到了错误消息(我也尝试了连续操作,但是它也不起作用)

Error in UseMethod("round_any") : 
  no applicable method for 'round_any' applied to an object of class "character"

我该怎么做才能使它正常工作?

1 个答案:

答案 0 :(得分:2)

我无法获取您的数据,并尝试尝试一下是否可行:

break_year_map_data_nona %>% 
  mutate(
    break_year = cut(value, breaks = c(1961, 1980, 1990, 2000, 2010, 2016), 
                                labels = c("1960-", "1980-", "1990-", "2000-", "2010-2016"))
  ) %>%
  ggplot(aes(long, lat, group = group, fill = break_year )) +
  geom_polygon() +
  coord_map(xlim=c(-180,180)) +
  theme_void() +
  labs(title = "", fill = "Break year")