2017-05-24 67 views
0

我想在列表dates的x轴上有自定义值,其中包含字符串格式的日期。 我对mpg的数据没有那么感兴趣,因为主列有数据结构,其中的值是整数,我不能在那里存在Posixct日期。如何在R ggplot figure x轴上拥有自定义列表标签?

Vars   variable  value 
1: 1    Leo  164 
... 

守则,电流输出在图1

library('ggplot2')  
str(mpg) 

dates <- c("1.1.2017", "1.2.2017", "1.3.2017", "2.4.2017", "10.5.2017", "12.5.2017", "13.5.2017") 

# TODO how to have here custom values on x-axis from dates? 
ggplot(mpg, aes(x = class, y = hwy)) + 
    geom_boxplot() 

你不能只是有x = dates因为dates不属于mpg

图1个与默认的x标签电流输出

enter image description here

预期输出:在图的x轴的那些7个日期。

R:如果你想保持在轴上的值

ggplot(mpg, aes(x = class, y = hwy)) + 
    geom_boxplot() + 
    scale_x_discrete(labels = dates) 

,使用scale_x_continuous相反,istance的:3.4.0(反向移植)
OS:Debian的8.7

回答

2

试试这个以下这保持刻度值:l,y轴

scale_y_continuous("Y Axis Title") 
+1

轴线是离散的,以便用'scale_x_discrete(“日期”中,标签=日期)' – johnckane

+0

你不想影响哪些值?轴标题?刻度标记标签?还有别的吗? – johnckane

+0

你可以用+ scale_y_continuous(“Y轴标题”,lim = c(20,40))' – johnckane