【问题标题】:Rename x tick marks not numeric (POSIXct) ggplot重命名x刻度线不是数字(POSIXct)ggplot
【发布时间】:2020-11-20 10:48:57
【问题描述】:

我想在我的图形中将 x ticks marks 重命名为 localtime"POSIXct" "POSIXt"

我原来的localtime是葡萄牙语,我想重命名为英文

Dez = 十二月

一月 = 一月

Fev = 二月

三月 = 三月

ps:因为有超过 2000 个本地时间,所以我无法举一个真实的例子来得到类似的情节结果。

dt = data.table(localtime= as.POSIXct(c("2016-10-24 12:45:06", "2016-10-24 12:46:13", "2016-10-24 12:47:02", "2016-10-24 12:48:27", "2016-10-24 12:52:39", "2016-10-24 12:55:11", "2016-10-30 21:08:02", "2016-10-30 21:18:27", "2016-10-30 21:30:13","2016-10-24 23:27:21", "2016-10-26 06:54:29")),
depth_range = c("shallow", "deep"),
dmean = c(30, 50, 200, 76, 467, 87, 98, 10, 240, 176, 89))

ggplot(dt, aes( x=localtime, xend= localtime, y=0, yend= dmean, color=depth_range))+
  geom_segment( size=1)+
  scale_colour_manual(name = "Dive category", values = c("grey70", "orangered3")) +
  scale_y_reverse()+
  xlab("Time")+
  ylab ("Dive depth (m)")+
  ggtitle("Whale ID 172001_17")+
  theme_bw()

我的数据的真实情节

我尝试了一些东西:

scale_x_discrete(labels=c("Dez"="December","Jan"="January", "Fev"="February", "Mar"= "March"))

scale_x_discrete(labels = c("December", "January", "February", "March"))

scale_x_discrete(breaks=c("Dez", "Jan", "Fev", "Mar"),
          labels=c("December", "January", "February", "March"))

scale_x_continuous(breaks=c("Dez", "Jan", "Fev", "Mar"),
                 labels=c("December", "January", "February", "March"))  

scale_x_continuous (names= c("December", "January", "February", "March"))

leg2<- c("December", "January", "February", "March")
scale_x_continuous(labels = leg2)

leg2<- c("December", "January", "February", "March")
scale_x_discrete(labels= leg2)

theme(axis.text.x = c("December", "January", "February", "March"))

theme(axis.text.x = element_text("December", "January", "February", "March"))

#I tried too to convert localtime to factor, but the plot didn't work well
ggplot(dt, aes( x=factor(localtime), xend= factor(localtime), y=0, yend= dmean, color=depth_range))+

显示该错误:

错误:mapped_discrete 对象只能从数字向量创建

我该如何解决这个问题??

【问题讨论】:

  • 试试+ scale_x_datetime(date_labels="%b"),它会用短短的月份缩写标记每个刻度。这将在 x 跨度较短时重复月份标签(如本例所示),因此 scale_x_datetime 的其他选项允许您指定放置刻度的位置(breaks=date_breaks=)。
  • 但问题是因为我的原始数据是葡萄牙语,我想重命名为英文。我编辑了帖子
  • 其中之一:(1)使用scale_x_datetime(breaks=..., labels=...);或(2)查看Sys.getlocale()(以保存当前值),然后查看Sys.setlocale("LC_ALL", "English") 并尝试绘图。 (那你可能想把它设置回原来的样子。)我看到其他人建议Sys.setenv(LANG="English"),不确定它是否会影响这个。
  • (1) 和 (3) 没有重命名... (2) 显示:Warning message: In Sys.setlocale ("LC_ALL", "English"): SO informs that the request to define the locale as 'English' cannot be honored
  • 试试Sys.setlocale(locale="English_United States.1252")(来自我的回答)。

标签: r ggplot2 plot rename xticks


【解决方案1】:

我已经用英语了,所以我将通过绘制葡萄牙语来演示:-)

首先,我将保存我当前的语言环境,因为我不会说葡萄牙语,有时 R 的错误在我自己的语言中可能会令人困惑:

Sys.getlocale()
# [1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
Sys.setlocale(locale="Portuguese")
# [1] "LC_COLLATE=Portuguese_Brazil.1252;LC_CTYPE=Portuguese_Brazil.1252;LC_MONETARY=Portuguese_Brazil.1252;LC_NUMERIC=C;LC_TIME=Portuguese_Brazil.1252"

现在,我绘制,添加scale_x_datetime

ggplot(dt, aes( x=localtime, xend= localtime, y=0, yend= dmean, color=depth_range))+
  geom_segment( size=1)+
  scale_colour_manual(name = "Dive category", values = c("grey70", "orangered3")) +
  scale_y_reverse()+
  xlab("Time")+
  ylab ("Dive depth (m)")+
  ggtitle("Whale ID 172001_17")+
  theme_bw() +
  scale_x_datetime(date_labels = "%b %d")

我包含了"%d",因为您的示例数据只跨越了几天;您应该可以只使用"%b"(由?strptime 确定)。

注意:这突出了一个可能的缺陷:ggplot2 不知道标签格式 "%b" 不比“1 个月”更明确,所以如果我不包括 @格式为 987654331@(月份中的某天),ggplot2 将在 x 轴上愉快地显示 out out out。在您的真实数据中,这可能不是问题。您可以使用其他选项,例如date_breaks="1 month",以确保 x 轴刻度不超过 1 个月……但在这里使用它不会导致刻度,因为我相信它默认为每月的第一天该样本在该日期没有任何内容。您可能需要在 scale 调用中硬编码 breaks= 以修复它们的去向,和/或使用中断函数来控制中断的去向。

【讨论】:

  • 谢谢,这段代码运行良好,但没有重命名:/。这里是Sys.getlocale() [1] "pt_BR.UTF-8/pt_BR.UTF-8/pt_BR.UTF-8/C/pt_BR.UTF-8/pt_BR.UTF-8"
  • 在绘图代码之前尝试Sys.setlocale(locale="English_United States.1252") 时发生了什么?
  • 只是不重命名刻度
  • 我不知道该说什么。在我的系统上,我显示英语,更改语言环境,然后显示葡萄牙语,所以前提是可靠的,也许你的数据还有其他内容?
  • Sys.setlocale(locale="English") 怎么样?这两者在我的(windows)系统上是同义词,也许它们对你来说足够不同?
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
相关资源
最近更新 更多