【发布时间】: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