Graphics Reference
In-Depth Information
library(gcookbook) # For the data set
csub <- subset(climate, Source == "Berkeley" & Year >= 1900 )
csub$pos <- csub$Anomaly10y >= 0
csub
Source Year Anomaly1y Anomaly5y Anomaly10y Unc10y
Berkeley 1900
NA
NA
-0.171 0.108 FALSE
Berkeley 1901
NA
NA
-0.162 0.109 FALSE
Berkeley 1902
NA
NA
-0.177 0.108 FALSE
...
Berkeley 2002
NA
NA
0.856 0.028 TRUE
Berkeley 2003
NA
NA
0.869 0.028 TRUE
Berkeley 2004
NA
NA
0.884 0.029 TRUE
Once we have the data, we can make the graph and map pos to the fill color, as in Figure 3-11 .
Notice that we use position="identity" with the bars. This will prevent a warning message
about stacking not being well defined for negative numbers:
ggplot(csub, aes(x = Year, y = Anomaly10y, fill = pos)) +
geom_bar(stat = "identity" , position = "identity" )
Figure 3-11. Different colors for positive and negative values
Discussion
There are a few problems with the first attempt. First, the colors are probably the reverse of what
we want: usually, blue means cold and red means hot. Second, the legend is redundant and dis-
tracting.
Search WWH ::




Custom Search