2017-05-24 70 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