R with complex data

I'm having trouble creating a complex barricade with aggregate data. When I use pivot tables from other people's reports, I usually use Excel, but I would like to start making all my charts in R, possibly with a grid or ggplot. In Excel, which executes a complex barcode of the following aggregated data, it takes a few clicks (Insert, Column Charts, Stacked Column), and you will get something like this. enter image description here .

Also, wanting this diagram in R, I also want to use the ggplot face, i.e. put the two folded velvet side by side in ggplot to compare the two groups (A and B). I played with other charts and this seems like the best choice. This is data. In an Excel chart, only Group A is displayed (numbers are percentages).

D<-as.data.frame(structure(list(Group = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B" ), Education = c("NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", "Below NVQ Level 2", "Other qualification", "No qualification", "NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", "Below NVQ Level 2", "Other qualification", "No qualification"), Full.Time = c(47, 27, 23, 17, 18, 9, 36, 26, 22, 22, 27, 12), PT.16.hours = c(20, 24, 22, 18, 18, 12, 22, 21, 21, 22, 14, 10), PT.16.hours.1 = c(12, 11, 10, 11, 13, 5, 24, 25, 25, 20, 16, 12)), .Names = c("Group", "Education", "Full.Time", "PT>16.hours", "PT<16.hours"))) 

Before proceeding to the cut to show the difference in the two groups, in fact, I have problems creating a separate histogram (for example, above) with ggplot2. I assume that I should not have 3 variables (FullTime, PT, PT> 16 hours), and single rows for each row, so instead

 A NVQ Level 4 and above 47 20 12 A NVQ Level3 27 24 11 

I had to

 Group Education Work Percentage A NVQ Level 4 and above Full Time 47 A NVQ Level 4 and above PT>16 hours 20 

If this is the only way to get ggplot to make a chart, how would you switch from one format to another with a few lines of code? I often find this data type, so it would be nice to have a standardized procedure. I also played with the ggplot "identity" option, but did not have much success.

Any help would be greatly appreciated.

thanks

+7
source share
2 answers

reshape your details:

 library(reshape2) df <- melt(D) 

And just speak it :)

 ggplot(df, aes(x = factor(Education), y = value, fill = factor(variable))) + geom_bar() + facet_grid(.~Group) + ylab('') + xlab('') + opts(title = '') + scale_fill_discrete('') + theme_bw() + opts(axis.text.x=theme_text(angle = 45, hjust = 1, vjust = 1)) 

If the first line creates aesthetics sets, the second line adds the bar and facet layer, on the third line we remove unnecessary texts from the graph, the 4th line sets the theme b&w and on the last line we rotate the x asis labels.

enter image description here

+8
source

The trick is to use melt from the plyr package to melt the three measured columns into one (a new column named value ) along with an identification column (named variable ) to group

 require(ggplot2) require(reshape) # first we need to get Full.Time, PT.16, etc. into one column df <- melt(D, .measure.vars=.(Full.Time, PT.16.hours, PT.16.hours.1)) ggplot(df, aes(x=Education, y=value, fill=variable )) + geom_bar(stat="identity") 

The rest is just reordering factors, so the output matches what you want.

Take a look at df to see that the melt is ending, as this is a normal workflow for ggplot2.

plot

To go to the facet plot using the Group coefficient, you just need to add the corresponding facet_wrap :

 ggplot(df, aes(x=Education, y=value, fill=variable )) + geom_bar(stat="identity") + facet_wrap(~ Group) 

facetted plot

+3
source

All Articles