3

Assuming is.character(MyColours) = TRUE where MyColours contains a number of hexadecimal colour values.

How do I ensure that when I want to plot using ggplot I can ensure that MyColours[1] will always be used to represent "Ford" and MyColour[3] will represent "Toyota" when I can't guarantee the order "Ford" or "Toyota" will appear in the observations I use to plot.

While the geo_bar example below was helpful and did remind me to add column names to MyColours I'm hoping to plot lines not bars

    ggplot(MyData, aes(x = TimePeriod, y = CountOfItems, 
group = Account, color = scale_fill_manual(values = MyColours))) 
    + geom_line(size = 1) 
    + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

I get the error Aesthetics must either be length one, or the same length as the dataProblems:scale_fill_manual(values = MyColours) even when length(MyColours) is equal to length(unique(MyData$Account))

Sample Data

dput(MyData)
structure(list(Account = structure(c(1L, 2L, 1L, 2L), .Label = c("Mazda RX4", 
"Toyota Corolla"), class = "factor"), CountOfItems = c(14, 120, 
23, 345), TimePeriod = structure(c(1L, 2L, 2L, 3L), .Label = c("2010-12", 
"2011-01", "2011-02"), class = "factor")), .Names = c("Account", 
"CountOfItems", "TimePeriod"), row.names = c(NA, -4L), class = "data.frame")

Is it possible to use scale_fill_manual with line plots?

mobcdi
  • 1,532
  • 2
  • 28
  • 49
  • Please provide a reproducible example when asking R questions: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lukeA Jul 09 '15 at 12:30

1 Answers1

5

You could name MyColours, e.g. like this:

MyColour <- c("#FF0000", "#00FF00", "#0000FF") 
names(MyColour) <- c("Mazda RX4", "Toyota Corolla", "Fiat 128")

df <- mtcars[c("Mazda RX4", "Toyota Corolla", "Fiat 128"), ]
df$car <- rownames(df)
library(ggplot2)
ggplot(df, aes(x = car, y = mpg, fill = car)) +
  geom_bar(stat = "identity") + 
  scale_fill_manual(values = MyColour)
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Edit your post and add a reproducible example. – lukeA Jul 09 '15 at 13:03
  • Updated question but still learning R and stackoverflow so apologies for forgetting the markdown. – mobcdi Jul 09 '15 at 15:11
  • No problem. I guess everyone went through that learning process here. When I run your code, I get - amongst other errors - `Error in ggplot(MyData, aes(x = TimePeriod, y = CountOfItems, group = Account, : object 'MyData' not found`. Please add it - or a similar data set. The link that I provided shows how. – lukeA Jul 09 '15 at 15:19
  • Sample data added where I want to color the lines in the plot of the data accounting to the `MyColour` so Mazda RX4 would use #FF0000 for its line colour – mobcdi Jul 09 '15 at 16:24
  • Try `ggplot(MyData, aes(x = TimePeriod, y = CountOfItems, group = Account, color = Account)) + geom_line(size = 1) + scale_color_manual(values = MyColours) ` – lukeA Jul 09 '15 at 16:25
  • That worked. Thanks @lukA. If I'm honest I'm not sure why though – mobcdi Jul 10 '15 at 13:27