I'm making a stacked bar chart with R.
# initialize a data frame
df_values_2012 <- data.frame(
workvalue = character ( )
, most = numeric ( )
, second = numeric ( )
, third = numeric ( )
, fourth = numeric ( )
, fifth = numeric ( )
, stringsAsFactors=FALSE
)
df_values_2012 [1 , ] <-
c ( "value01" ,
7L, 25L, 46L, 17L, 5L)
df_values_2012 [2 , ] <-
c ( "value02" ,
15L, 29L, 21L, 21L, 14L)
df_values_2012 [3 , ] <-
c ( "value03" ,
27L, 8L, 7L, 12L, 46L )
df_values_2012 [4 , ] <-
c ( "value04" ,
28L, 16L, 13L, 26L, 17L )
df_values_2012 [5 , ] <-
c ( "value05" ,
23L, 22L, 13L, 25L, 17L )`
df_values_2012
workvalue most second third fourth fifth
1 value01 7 25 46 17 5
2 value02 15 29 21 21 14
3 value03 27 8 7 12 46
4 value04 28 16 13 26 17
5 value05 23 22 13 25 17
You will note that -both- the rows and the columns add up to 100.
I've been making a graph of the results. However, I'm having trouble with the legend.
barplot ( as.matrix ( df_values_2012 [ , 2:6 ] )
, col=c("blue", "green", "red", "yellow", "cyan")
)
barplot ( as.matrix ( df_values_2012 [ , 2:6 ] )
, col=c("blue", "green", "red", "yellow", "cyan")
, legend.text = df_values_2012 [ , 1]
)
barplot ( as.matrix ( df_values_2012 [ , 2:6 ] )
, col=c("blue", "green", "red", "yellow", "cyan")
, legend.text = df_values_2012 [ , 1]
, args.legend = "topleft"
)
The legend is on top of the graph. It's hard to make out.
Is there a way to put the legend on the side? Perhaps, by using mosaicplot, or ggplot?
What I've tried so far has not been successful. And poking through the docs takes a long time.
Thanks a lot!