使用ggplot2

时间:2018-03-08 12:46:03

标签: r ggplot2

我正在使用以下内容;它是3 ggplots的列表。但是当我去打印时plotListBar[["HOG"]] R显示所有3 ggplot图表,但我想打印HOG。我在代码中哪里出错了,以便我能够单独打印ggplots?目前,它正在并排打印所有3个地块。

symbols <- c("HOG", "GE", "GOOG")
plotListBar <- list()
for(symbol in symbols){
  plotListBar[[symbol]] <- ggplot(mm, aes(Date, value,  fill = variable)) +  facet_wrap(~ Ticker) +
    geom_bar(stat="identity", position = "dodge")
}

清单结构:

List of 3
 $ HOG :List of 9
  ..$ data       :'data.frame': 36 obs. of  4 variables:
  .. ..$ Ticker  : chr [1:36] "GOOG" "GOOG" "GOOG" "GOOG" ...
  .. ..$ Date    : num [1:36] 2017 2016 2015 2014 2017 ...
  .. ..$ variable: Factor w/ 6 levels "CollectionPeriod",..: 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ value   : num [1:36] 61.6 57.5 67.7 60 73.1 ...
  ..$ layers     :List of 1
  .. ..$ :Classes 'LayerInstance', 'Layer', 'ggproto' <ggproto object: Class LayerInstance, Layer>
    aes_params: list
    compute_aesthetics: function
    compute_geom_1: function
    compute_geom_2: function
    compute_position: function
    compute_statistic: function
    data: waiver
    draw_geom: function
    finish_statistics: function
    geom: <ggproto object: Class GeomBar, GeomRect, Geom>
        aesthetics: function
        default_aes: uneval
        draw_group: function
        draw_key: function
        draw_layer: function
        draw_panel: function
        extra_params: na.rm
        handle_na: function
        non_missing_aes: 
        optional_aes: 
        parameters: function
        required_aes: x y
        setup_data: function
        use_defaults: function
        super:  <ggproto object: Class GeomRect, Geom>
    geom_params: list
    inherit.aes: TRUE
    layer_data: function
    map_statistic: function
    mapping: NULL
    position: <ggproto object: Class PositionDodge, Position>
        compute_layer: function
        compute_panel: function
        required_aes: x
        setup_data: function
        setup_params: function
        width: NULL
        super:  <ggproto object: Class Position>
    print: function
    show.legend: NA
    stat: <ggproto object: Class StatIdentity, Stat>
        aesthetics: function
        compute_group: function
        compute_layer: function
        compute_panel: function
        default_aes: uneval
        extra_params: na.rm
        finish_layer: function
        non_missing_aes: 
        parameters: function
        required_aes: 
        retransform: TRUE
        setup_data: function
        setup_params: function
        super:  <ggproto object: Class Stat>
    stat_params: list
    subset: NULL
    super:  <ggproto object: Class Layer> 
  ..$ scales     :Classes 'ScalesList', 'ggproto' <ggproto object: Class ScalesList>
    add: function
    clone: function
    find: function
    get_scales: function
    has_scale: function
    input: function
    n: function
    non_position_scales: function
    scales: list
    super:  <ggproto object: Class ScalesList> 
  ..$ mapping    :List of 3
  .. ..$ x   : symbol Date
  .. ..$ y   : symbol value
  .. ..$ fill: symbol variable
  ..$ theme      : list()
  ..$ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto' <ggproto object: Class CoordCartesian, Coord>
    aspect: function
    distance: function
    expand: TRUE
    is_linear: function
    labels: function
    limits: list
    range: function
    render_axis_h: function
    render_axis_v: function
    render_bg: function
    render_fg: function
    train: function
    transform: function
    super:  <ggproto object: Class CoordCartesian, Coord> 
  ..$ facet      :Classes 'FacetWrap', 'Facet', 'ggproto' <ggproto object: Class FacetWrap, Facet>
    compute_layout: function
    draw_back: function
    draw_front: function
    draw_labels: function
    draw_panels: function
    finish_data: function
    init_scales: function
    map: function
    map_data: function
    params: list
    render_back: function
    render_front: function
    render_panels: function
    setup_data: function
    setup_params: function
    shrink: TRUE
    train: function
    train_positions: function
    train_scales: function
    super:  <ggproto object: Class FacetWrap, Facet> 
  ..$ plot_env   :<environment: R_GlobalEnv> 
  ..$ labels     :List of 3
  .. ..$ x   : chr "Date"
  .. ..$ y   : chr "value"
  .. ..$ fill: chr "variable"
  ..- attr(*, "class")= chr [1:2] "gg" "ggplot"
 $ GE  :List of 9
  ..$ data       :'data.frame': 36 obs. of  4 variables:
  .. ..$ Ticker  : chr [1:36] "GOOG" "GOOG" "GOOG" "GOOG" ...
  .. ..$ Date    : num [1:36] 2017 2016 2015 2014 2017 ...
  .. ..$ variable: Factor w/ 6 levels "CollectionPeriod",..: 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ value   : num [1:36] 61.6 57.5 67.7 60 73.1 ...
  ..$ layers     :List of 1
  .. ..$ :Classes 'LayerInstance', 'Layer', 'ggproto' <ggproto object: Class LayerInstance, Layer>
    aes_params: list
    compute_aesthetics: function
    compute_geom_1: function
    compute_geom_2: function
    compute_position: function
    compute_statistic: function
    data: waiver
    draw_geom: function
    finish_statistics: function
    geom: <ggproto object: Class GeomBar, GeomRect, Geom>
        aesthetics: function
        default_aes: uneval
        draw_group: function
        draw_key: function
        draw_layer: function
        draw_panel: function
        extra_params: na.rm
        handle_na: function
        non_missing_aes: 
        optional_aes: 
        parameters: function
        required_aes: x y
        setup_data: function
        use_defaults: function
        super:  <ggproto object: Class GeomRect, Geom>
    geom_params: list
    inherit.aes: TRUE
    layer_data: function
    map_statistic: function
    mapping: NULL
    position: <ggproto object: Class PositionDodge, Position>
        compute_layer: function
        compute_panel: function
        required_aes: x
        setup_data: function
        setup_params: function
        width: NULL
        super:  <ggproto object: Class Position>
    print: function
    show.legend: NA
    stat: <ggproto object: Class StatIdentity, Stat>
        aesthetics: function
        compute_group: function
        compute_layer: function
        compute_panel: function
        default_aes: uneval
        extra_params: na.rm
        finish_layer: function
        non_missing_aes: 
        parameters: function
        required_aes: 
        retransform: TRUE
        setup_data: function
        setup_params: function
        super:  <ggproto object: Class Stat>
    stat_params: list
    subset: NULL
    super:  <ggproto object: Class Layer> 
  ..$ scales     :Classes 'ScalesList', 'ggproto' <ggproto object: Class ScalesList>
    add: function
    clone: function
    find: function
    get_scales: function
    has_scale: function
    input: function
    n: function
    non_position_scales: function
    scales: list
    super:  <ggproto object: Class ScalesList> 
  ..$ mapping    :List of 3
  .. ..$ x   : symbol Date
  .. ..$ y   : symbol value
  .. ..$ fill: symbol variable
  ..$ theme      : list()
  ..$ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto' <ggproto object: Class CoordCartesian, Coord>
    aspect: function
    distance: function
    expand: TRUE
    is_linear: function
    labels: function
    limits: list
    range: function
    render_axis_h: function
    render_axis_v: function
    render_bg: function
    render_fg: function
    train: function
    transform: function
    super:  <ggproto object: Class CoordCartesian, Coord> 
  ..$ facet      :Classes 'FacetWrap', 'Facet', 'ggproto' <ggproto object: Class FacetWrap, Facet>
    compute_layout: function
    draw_back: function
    draw_front: function
    draw_labels: function
    draw_panels: function
    finish_data: function
    init_scales: function
    map: function
    map_data: function
    params: list
    render_back: function
    render_front: function
    render_panels: function
    setup_data: function
    setup_params: function
    shrink: TRUE
    train: function
    train_positions: function
    train_scales: function
    super:  <ggproto object: Class FacetWrap, Facet> 
  ..$ plot_env   :<environment: R_GlobalEnv> 
  ..$ labels     :List of 3
  .. ..$ x   : chr "Date"
  .. ..$ y   : chr "value"
  .. ..$ fill: chr "variable"
  ..- attr(*, "class")= chr [1:2] "gg" "ggplot"
 $ GOOG:List of 9
  ..$ data       :'data.frame': 36 obs. of  4 variables:
  .. ..$ Ticker  : chr [1:36] "GOOG" "GOOG" "GOOG" "GOOG" ...
  .. ..$ Date    : num [1:36] 2017 2016 2015 2014 2017 ...
  .. ..$ variable: Factor w/ 6 levels "CollectionPeriod",..: 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ value   : num [1:36] 61.6 57.5 67.7 60 73.1 ...
  ..$ layers     :List of 1
  .. ..$ :Classes 'LayerInstance', 'Layer', 'ggproto' <ggproto object: Class LayerInstance, Layer>
    aes_params: list
    compute_aesthetics: function
    compute_geom_1: function
    compute_geom_2: function
    compute_position: function
    compute_statistic: function
    data: waiver
    draw_geom: function
    finish_statistics: function
    geom: <ggproto object: Class GeomBar, GeomRect, Geom>
        aesthetics: function
        default_aes: uneval
        draw_group: function
        draw_key: function
        draw_layer: function
        draw_panel: function
        extra_params: na.rm
        handle_na: function
        non_missing_aes: 
        optional_aes: 
        parameters: function
        required_aes: x y
        setup_data: function
        use_defaults: function
        super:  <ggproto object: Class GeomRect, Geom>
    geom_params: list
    inherit.aes: TRUE
    layer_data: function
    map_statistic: function
    mapping: NULL
    position: <ggproto object: Class PositionDodge, Position>
        compute_layer: function
        compute_panel: function
        required_aes: x
        setup_data: function
        setup_params: function
        width: NULL
        super:  <ggproto object: Class Position>
    print: function
    show.legend: NA
    stat: <ggproto object: Class StatIdentity, Stat>
        aesthetics: function
        compute_group: function
        compute_layer: function
        compute_panel: function
        default_aes: uneval
        extra_params: na.rm
        finish_layer: function
        non_missing_aes: 
        parameters: function
        required_aes: 
        retransform: TRUE
        setup_data: function
        setup_params: function
        super:  <ggproto object: Class Stat>
    stat_params: list
    subset: NULL
    super:  <ggproto object: Class Layer> 
  ..$ scales     :Classes 'ScalesList', 'ggproto' <ggproto object: Class ScalesList>
    add: function
    clone: function
    find: function
    get_scales: function
    has_scale: function
    input: function
    n: function
    non_position_scales: function
    scales: list
    super:  <ggproto object: Class ScalesList> 
  ..$ mapping    :List of 3
  .. ..$ x   : symbol Date
  .. ..$ y   : symbol value
  .. ..$ fill: symbol variable
  ..$ theme      : list()
  ..$ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto' <ggproto object: Class CoordCartesian, Coord>
    aspect: function
    distance: function
    expand: TRUE
    is_linear: function
    labels: function
    limits: list
    range: function
    render_axis_h: function
    render_axis_v: function
    render_bg: function
    render_fg: function
    train: function
    transform: function
    super:  <ggproto object: Class CoordCartesian, Coord> 
  ..$ facet      :Classes 'FacetWrap', 'Facet', 'ggproto' <ggproto object: Class FacetWrap, Facet>
    compute_layout: function
    draw_back: function
    draw_front: function
    draw_labels: function
    draw_panels: function
    finish_data: function
    init_scales: function
    map: function
    map_data: function
    params: list
    render_back: function
    render_front: function
    render_panels: function
    setup_data: function
    setup_params: function
    shrink: TRUE
    train: function
    train_positions: function
    train_scales: function
    super:  <ggproto object: Class FacetWrap, Facet> 
  ..$ plot_env   :<environment: R_GlobalEnv> 
  ..$ labels     :List of 3
  .. ..$ x   : chr "Date"
  .. ..$ y   : chr "value"
  .. ..$ fill: chr "variable"
  ..- attr(*, "class")= chr [1:2] "gg" "ggplot"

Dput示例代码

structure(list(Ticker = c("GOOG", "GOOG", "GOOG", "GOOG", "GE", 
"GE", "GE", "GE", "HOG", "HOG", "HOG", "HOG", "GOOG", "GOOG", 
"GOOG", "GOOG", "GE", "GE", "GE", "GE", "HOG", "HOG", "HOG", 
"HOG", "GOOG", "GOOG", "GOOG", "GOOG", "GE", "GE", "GE", "GE", 
"HOG", "HOG", "HOG", "HOG"), Date = c(2017, 2016, 2015, 2014, 
2017, 2016, 2015, 2014, 2017, 2016, 2015, 2014, 2017, 2016, 2015, 
2014, 2017, 2016, 2015, 2014, 2017, 2016, 2015, 2014, 2017, 2016, 
2015, 2014, 2017, 2016, 2015, 2014, 2017, 2016, 2015, 2014), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("CollectionPeriod", 
    "DaysofInventory", "DaysofPayable", "CashCollectionCycle", 
    "DaysofPayableAccExp", "CashCollectionCycleAccExp"), class = "factor"), 
    value = c(61.5878850750981, 57.5447536334633, 67.7003960580885, 
    59.9973485250224, 73.0579967729518, 71.0453384212398, 84.0229160454913, 
    72.3776710131076, 157.424759439158, 143.734811872338, 140.084289622044, 
    126.828872394842, 5.99752100563807, 2.78388069895839, 0, 
    0, 84.1604875945267, 89.3934679478049, 96.688883921218, 75.6542499589885, 
    57.0743314700106, 50.7784397810473, 60.7847464805146, 44.1958495635379, 
    -25.119123357392, -21.2011212931869, -25.0253870188894, -24.7293880614704, 
    -58.1710472344051, -57.7254500235557, -58.7476763065628, 
    -51.6094654449157, -24.1362278754634, -23.9021892488319, 
    -24.4431638276767, -19.3838681657801)), .Names = c("Ticker", 
"Date", "variable", "value"), row.names = c(NA, 36L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

由于您没有提供示例数据,我使用了mtcars

symbols <- c("HOG", "GE", "GOOG")
plotListBar <- list()
for(symbol in symbols){
  plotListBar[[symbol]] <- ggplot(mtcars, aes(hp, disp)) + geom_point() + ggtitle(symbol)

}
# this is producing one plot as wanted. 
plotListBar[[1]]
plotListBar$GE
plotListBar[["GOOG"]]

答案 1 :(得分:1)

列表中的每个组件都有相同的数据:

请参阅:

plotListBar[["GOOG"]]$data$Ticker
# [1] "GOOG" "GOOG" "GOOG" "GOOG" "GE"   "GE"   "GE"   "GE"   "HOG"  "HOG"  "HOG"  "HOG"  "GOOG"
# [14] "GOOG" "GOOG" "GOOG" "GE"   "GE"   "GE"   "GE"   "HOG"  "HOG"  "HOG"  "HOG"  "GOOG" "GOOG"
# [27] "GOOG" "GOOG" "GE"   "GE"   "GE"   "GE"   "HOG"  "HOG"  "HOG"  "HOG" 

尝试:

symbols <- c("HOG", "GE", "GOOG")
plotListBar <- list()
for(symbol in symbols){
  plotListBar[[symbol]] <- ggplot(mm[mm$Ticker == symbol,], aes(Date, value,  fill = variable)) + facet_wrap(~ Ticker) +
    geom_bar(stat="identity", position = "dodge")
}

现在plotListBar[["GOOG"]]只会策划GOOG