1

Maybe this is a very basic question but I'm a ggplot and R beginner.

I'm using this command to get a barplot:

ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
  geom_bar(width=.8, stat="identity") +
  xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") + 
  theme(plot.title=element_text(face="bold", size=12)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
  theme(axis.text.y = element_text(size=10))

I want to change the colors of barplot, but keeping the gradient of the colors depending on value column. I've tried this but I lose the gradient:

ggplot(data=melt, aes(x=variable, y=value, fill=value)) + 
  geom_bar(width=.8, stat="identity", fill="red") +
  xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") + 
  theme(plot.title=element_text(face="bold", size=12)) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) + 
  theme(axis.text.y = element_text(size=10))

The data is simple, only two columns( variable - value ):

variable    value
1   nu73    13576.49
2   nu73t   10891.88
3   nu81    12673.33
4   nu81t   12159.91
5   nu83    12570.82
6   nu83t   11828.04

Thank you guys in advance

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
cucurbit
  • 1,422
  • 1
  • 13
  • 32

1 Answers1

2

You want to adjust the scale, in particular the continuous scale of fill colors, hence the function scale_fill_continuous().

ggplot(data = melt, aes(x = variable, y = value, fill = value)) +
    geom_bar(width = .8, stat = "identity") +
    labs(x = "Samples", y = "Expression", title = "Gapdh") + 
    theme(plot.title = element_text(face = "bold", size = 12),
          axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
          axis.text.y = element_text(size = 10)) +
    scale_fill_continuous(low = "firebrick4", high = "firebrick1")

(I slightly modified your plotting code: you can call theme once with multiple arguments, and I find labs nicer than a bunch of individual labeling calls.)

One other option is to use the palettes from the RColorBrewer package (which are incorporated into ggplot2). The scale_fill_brewer() scale if for discrete color scales, you can "distill" them into continuous scales with scale_fill_distiller(). For example

scale_fill_distiller(type = "seq", palette = "Reds")

To see all of the available scales, run RColorBrewer::display.brewer.all().

thothal
  • 16,690
  • 3
  • 36
  • 71
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294