0

我想在我的图形中重命名x ticks markswith name 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对象只能从数字向量创建

我怎样才能解决这个问题??

4

1 回答 1

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"是因为您的样本数据只跨越了几天;你应该能够使用just "%b"(由 决定?strptime)。

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

葡萄牙语月份的情节

于 2020-07-30T16:54:50.470 回答