ggplot2从geom_bar facet_wrap中删除刻度标记

时间:2015-12-18 20:38:43

标签: r ggplot2

如何在ggplot2中调整geom_bar facet_wrap图的y-tick标签?我尝试了几个选项,比如scale_y但没有成功......

Link to plot

# Plot ---------------------------------------------------------------------
p <- ggplot() + 
  geom_bar(data=uws, aes(y = Costs, x = Scenario, fill = Component), stat="identity",position='stack') + 
  ggtitle("Plot Title") +
  theme_bw() +  facet_wrap( ~ PE) # For facet choose between grid or wrap 

CSV文件(uws)如下所示:

> dput(uws)
structure(list(Scenario = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L), .Label = c("Conv", "S2", "S3", "S4"), class = "factor"), 
    Component = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L), .Label = c("WWC", "WWT"), class = "factor"), 
    PE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L), .Label = c("5,000", "10,000", "50,000", "100,000", 
    "500,000", "1,000,000", "1,500,000", "2,000,000", "2,500,000"
    ), class = "factor"), Costs = structure(c(28L, 61L, 1L, 45L, 
    18L, 38L, 34L, 4L, 54L, 7L, 1L, 62L, 39L, 52L, 59L, 12L, 
    29L, 33L, 1L, 24L, 19L, 16L, 36L, 1L, 56L, 49L, 1L, 43L, 
    40L, 27L, 57L, 60L, 30L, 14L, 1L, 11L, 20L, 2L, 35L, 22L, 
    55L, 26L, 1L, 25L, 41L, 9L, 58L, 31L, 5L, 32L, 1L, 37L, 51L, 
    15L, 6L, 44L, 8L, 42L, 1L, 46L, 63L, 21L, 10L, 48L, 13L, 
    47L, 1L, 50L, 3L, 23L, 17L, 53L), .Label = c("0", "100,877,653", 
    "1,161,742,099", "11,782,594", "1,184,976,941", "1,303,474,636", 
    "14,457,980", "1,579,969,255", "160,933,883", "1,737,966,181", 
    "182,380,189", "18,795,374", "1,974,961,569", "201,755,306", 
    "211,502,167", "21,379,682", "2,172,457,726", "2,323,484", 
    "23,234,842", "232,348,420", "256,752,017", "262,281,898", 
    "298,418,508", "30,393,724", "320,821,078", "321,867,765", 
    "34,105,538", "3,949,923", "39,499,231", "394,992,314", "418,428,095", 
    "423,004,335", "42,759,364", "4,344,915", "434,491,545", 
    "43,449,155", "449,044,916", "4,531,767", "4,646,968", "46,469,684", 
    "464,696,840", "513,504,035", "51,472,656", "549,905,635", 
    "5,617,335", "571,548,537", "596,837,015", "667,555,245", 
    "68,211,076", "690,195,107", "697,045,260", "7,228,990", 
    "775,888,120", "7,899,846", "789,984,628", "78,998,463", 
    "86,898,309", "868,983,090", "8,689,831", "88,674,399", "9,063,534", 
    "9,260,007", "929,393,680"), class = "factor")), .Names = c("Scenario", 
"Component", "PE", "Costs"), row.names = c(NA, -72L), class = "data.frame")

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的。访问您的数据非常有用。

如果查看数据集的结构,您会发现Cost是一个因素。只需将其更改为数字即可。如果仍有必要,它会更容易改变你的刻度

str(uws)
'data.frame':   72 obs. of  4 variables:
 $ Scenario : Factor w/ 4 levels "Conv","S2","S3",..: 1 2 3 4 1 2 3 4 1 2 ...
 $ Component: Factor w/ 2 levels "WWC","WWT": 1 1 1 1 2 2 2 2 1 1 ...
 $ PE       : Factor w/ 9 levels "5,000","10,000",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ Costs    : Factor w/ 63 levels "0","100,877,653",..: 28 61 1 45 18 38 34 4 54 7 ...

p <- ggplot() + 
  geom_bar(data=uws, aes(y = as.numeric(Costs), x = Scenario, fill = Component), stat="identity",position='stack') + 
  ggtitle("Plot Title") + ylab("Cost") +
  theme_bw() +  facet_wrap( ~ PE)

enter image description here