scale_x_continuous()和scale_x_discrete()在R中不起作用

时间:2016-06-21 11:54:06

标签: r

我想将直方图的x轴分成31个单位。所以我使用qplot()代码:

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我遇到了这个问题,因为我忘记在scale_x_discrete之前添加'+'。添加'+'将其固定。