2016-06-21 1566 views
-2

我想將我的直方圖的x軸分解爲31個單位。所以我用用qplot()的代碼:)scale_x_continuous()和scale_x_discrete()在R中不起作用

qplot(x = dob_day, data = pf) + 
    scale_x_discrete(breaks = 1:31) 

並採用ggplot(:

ggplot(data = pf, aes(x = dob_day)) + 
    geom_histogram(binwidth = 1) + 
    scale_x_discrete(breaks = 1:31) 

但無論哪種代碼不更新的情節。相反,控制檯顯示

scale_x_discrete(breaks = 1:31) 
ggproto object: Class ScaleDiscretePosition, ScaleDiscrete, Scale> 
aesthetics: x xmin xmax xend 
break_info: function 
break_positions: function 
breaks: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ... 
call: call 
clone: function 
dimension: function 
drop: TRUE 
expand: waiver 
get_breaks: function 
get_breaks_minor: function 
get_labels: function 
get_limits: function 
guide: none 
is_discrete: function 
is_empty: function 
labels: waiver 
limits: NULL 
map: function 
map_df: function 
na.value: NA 
name: waiver 
palette: function 
range: <ggproto object: Class RangeDiscrete, Range> 
    range: NULL 
    reset: function 
    train: function 
    super: <ggproto object: Class RangeDiscrete, Range> 
range_c: <ggproto object: Class RangeContinuous, Range> 
    range: NULL 
    reset: function 
    train: function 
    super: <ggproto object: Class RangeContinuous, Range> 
reset: function 
scale_name: position_d 
train: function 
train_df: function 
transform: function 
transform_df: function 
super: <ggproto object: Class ScaleDiscretePosition, ScaleDiscrete, Scale> 

同樣的,scale_x_continuous()在控制檯顯示情況:

> scale_x_continuous(breaks = seq(1, 7, 1), limits = c(0, 7)) 
<ScaleContinuousPosition> 
Range: 
Limits: 0 -- 7 
+0

我不相信你。 'ggplot(data = pf,aes(x = dob_day))+ geom_histogram(binwidth = 1)+ scale_x_discrete(breaks = 1:31)'不會給出這個輸出。有些東西你沒有告訴我們。請提供一個可重複的例子。 (你最後顯示的是另一個東西,在那裏你不用'+'把刻度添加到繪圖中。) – Roland

+0

Right ...'scale _ *()'函數返回一個無用的'ggproto'對象本身(大多數情況下)。你得到的輸出是如果你自己運行'scale _ *()'函數會發生什麼。您需要將其添加到情節纔能有用。 – Gregor

+0

@羅蘭,相信我或者不信,這就是發生的事情。我試圖在RMD和它的工作中運行相同的代碼! – Mehul007

回答

1

讓它重現性。當您運行scale_x_continuous(breaks = seq(1,7,1),limits = c(0,7))時,控制檯會提供該輸出而不將其應用於某些內容。試試這個:

gg <- ggplot(data = pf, aes(x = dob_day)) 
    gg <- gg + geom_histogram(binwidth = 1) + 
      scale_x_discrete(breaks = 1:31) 
+0

不工作!但是相同的代碼在RMD中工作! – Mehul007