-5

How can adjust y-tick labels for a geom_bar facet_wrap plot in ggplot2? I tried several options such as scale_y but didn't succeed ...

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 

The CSV file (uws) looks as follows:

> 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")

Any help would be much appreciated!

Stücke
  • 868
  • 3
  • 14
  • 41

1 Answers1

0

I think this what you want. Having access to your data was really useful.

if you look at the structure of your dataset, you will see that Cost is a factor. Just change it to numeric and it will work. it will easier to change your tick if still necessary

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

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • First of all thank you for your response! I was able to reproduce your graph (not too hard I admit). But how can I get the actual costs on the y scale? I think now it's percentages, or not? – Stücke Dec 20 '15 at 12:00
  • just remove the decimals from your cost, e.g.instead of 100,877,653, use 100877653, or take a look at this http://stackoverflow.com/questions/6123378/how-to-read-in-numbers-with-a-comma-as-decimal-separator – MLavoie Dec 20 '15 at 12:06
  • Thanks for your response. I tried sep="" and dec="" in a few variations but somehow the column "PE" is not loaded properly anymore. All fields now say "NA" ... hence the graph is messed up. Any idea why this could be the case? – Stücke Dec 20 '15 at 15:08
  • I removed this part 'uws$PE <- factor(uws$PE, levels = c("5,000", "10,000", "50,000", "100,000", "500,000", "1,000,000", "1,500,000","2,000,000", "2,500,000"))' ... now it's working. Thanks for your help! – Stücke Dec 21 '15 at 19:56