G.Maubach at weinwolf.de
2017-Mar-28 13:05 UTC
[R] Way to Plot Multiple Variables and Change Color
Hi All, in my current project I have to plot a whole bunch of related variables (item batteries, e.g. How do you rate ... a) Accelaration, b) Horse Power, c) Color Palette, etc.) which are all rated on a scale from 1 .. 4. I need to present the results as stacked bar charts where the variables are columns and the percentages of the scales values (1 .. 4) are the chunks of the stacked bar for each variable. To do this I have transformed my data from wide to long and calculated the percentage for each variable and value. The code for this is as follows: -- cut -- dfr <- structure( list( v07_01 = c(3, 1, 1, 4, 3, 4, 4, 1, 3, 2, 2, 3, 4, 4, 4, 1, 1, 3, 3, 4), v07_02 = c(1, 2, 1, 1, 2, 1, 4, 1, 1, 4, 4, 1, 4, 4, 1, 3, 2, 3, 3, 1), v07_03 = c(3, 2, 2, 1, 4, 1, 2, 3, 3, 1, 4, 2, 3, 1, 4, 1, 4, 2, 2, 3), v07_04 = c(3, 1, 1, 4, 2, 4, 4, 2, 2, 2, 4, 1, 2, 1, 3, 1, 2, 4, 1, 4), v07_05 = c(1, 2, 2, 2, 4, 4, 1, 1, 4, 4, 2, 1, 2, 1, 4, 1, 2, 4, 1, 4), v07_06 = c(1, 2, 1, 2, 1, 1, 3, 4, 3, 2, 2, 3, 3, 2, 4, 2, 3, 1, 4, 3), v07_07 = c(3, 2, 3, 3, 1, 1, 3, 3, 4, 4, 1, 3, 1, 3, 2, 4, 1, 2, 3, 4), v07_08 = c(3, 2, 1, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 3, 1, 4, 2, 2, 4), cased_id = structure( 1:20, .Label = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" ), class = "factor" ) ), .Names = c( "v07_01", "v07_02", "v07_03", "v07_04", "v07_05", "v07_06", "v07_07", "v07_08", "cased_id" ), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame") ) mdf <- melt(df) d_result <- mdf %>% dplyr::group_by(variable) %>% count(value) ggplot( d_result, aes(variable, y = n, fill = value)) + geom_bar(stat = "identity") + coord_cartesian(ylim = c(0,100)) -- cut -- Is there an easier way of doing this, i. e. a way without need to transform the data? How can I change the colors for the data points 1 .. 4? I tried -- cut -- d_result, aes(variable, y = n, fill = value)) + geom_bar(stat = "identity") + coord_cartesian(ylim = c(0,100)) + scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) -- cut - but this does not work cause I am mixing continuous and descrete values. How can I change the colors for the bars? Kind regards Georg
Hi Georg, I am a little unsure of what you want to do, but maybe this: mdf <- melt(dfr) d_result <- mdf %>% dplyr::group_by(variable, value) %>% summarise(n = n()) ggplot( d_result, aes(variable, y = n, fill = value)) + geom_bar(stat = "identity") HTH Ulrik On Tue, 28 Mar 2017 at 15:11 <G.Maubach at weinwolf.de> wrote:> Hi All, > > in my current project I have to plot a whole bunch of related variables > (item batteries, e.g. How do you rate ... a) Accelaration, b) Horse Power, > c) Color Palette, etc.) which are all rated on a scale from 1 .. 4. > > I need to present the results as stacked bar charts where the variables > are columns and the percentages of the scales values (1 .. 4) are the > chunks of the stacked bar for each variable. To do this I have transformed > my data from wide to long and calculated the percentage for each variable > and value. The code for this is as follows: > > -- cut -- > > dfr <- structure( > list( > v07_01 = c(3, 1, 1, 4, 3, 4, 4, 1, 3, 2, 2, 3, > 4, 4, 4, 1, 1, 3, 3, 4), > v07_02 = c(1, 2, 1, 1, 2, 1, 4, 1, 1, > 4, 4, 1, 4, 4, 1, 3, 2, 3, 3, 1), > v07_03 = c(3, 2, 2, 1, 4, 1, > 2, 3, 3, 1, 4, 2, 3, 1, 4, 1, 4, 2, 2, 3), > v07_04 = c(3, 1, 1, > 4, 2, 4, 4, 2, 2, 2, 4, 1, 2, 1, 3, 1, 2, 4, 1, 4), > v07_05 = c(1, > 2, 2, 2, 4, 4, 1, 1, 4, 4, 2, 1, 2, 1, 4, 1, 2, 4, 1, 4), > v07_06 = c(1, > 2, 1, 2, 1, 1, 3, 4, 3, 2, 2, 3, 3, 2, 4, 2, 3, 1, 4, 3), > v07_07 = c(3, > 2, 3, 3, 1, 1, 3, 3, 4, 4, 1, 3, 1, 3, 2, 4, 1, 2, 3, 4), > v07_08 = c(3, > 2, 1, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 3, 1, 4, 2, 2, 4), > cased_id = structure( > 1:20, > .Label = c( > "1", > "2", > "3", > "4", > "5", > "6", > "7", > "8", > "9", > "10", > "11", > "12", > "13", > "14", > "15", > "16", > "17", > "18", > "19", > "20" > ), > class = "factor" > ) > ), > .Names = c( > "v07_01", > "v07_02", > "v07_03", > "v07_04", > "v07_05", > "v07_06", > "v07_07", > "v07_08", > "cased_id" > ), > row.names = c(NA, -20L), > class = c("tbl_df", "tbl", > "data.frame") > ) > > mdf <- melt(df) > d_result <- mdf %>% > dplyr::group_by(variable) %>% > count(value) > > ggplot( > d_result, > aes(variable, y = n, fill = value)) + > geom_bar(stat = "identity") + > coord_cartesian(ylim = c(0,100)) > > -- cut -- > > Is there an easier way of doing this, i. e. a way without need to > transform the data? > > How can I change the colors for the data points 1 .. 4? > > I tried > > -- cut -- > > d_result, > aes(variable, y = n, fill = value)) + > geom_bar(stat = "identity") + > coord_cartesian(ylim = c(0,100)) + > scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) > > -- cut - > > but this does not work cause I am mixing continuous and descrete values. > > How can I change the colors for the bars? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Richard M. Heiberger
2017-Mar-28 15:41 UTC
[R] Way to Plot Multiple Variables and Change Color
I think you are looking for the likert function in the HH package.>From ?likertDiverging stacked barcharts for Likert, semantic differential, rating scale data, and population pyramids. This will get you started. Much more fine control is available. See the examples and demo. ## install.packages("HH") ## if not yet on your system. library(HH) AA <- dfr[,-9] labels <- sort(unique(as.vector(data.matrix(AA)))) result.template <- integer(length(labels)) names(result.template) <- labels BB <- apply(AA, 2, function(x, result=result.template) { tx <- table(x) result[names(tx)] <- tx result } ) BB likert(t(BB), ReferenceZero=0, horizontal=FALSE) On Tue, Mar 28, 2017 at 6:05 AM, <G.Maubach at weinwolf.de> wrote:> Hi All, > > in my current project I have to plot a whole bunch of related variables > (item batteries, e.g. How do you rate ... a) Accelaration, b) Horse Power, > c) Color Palette, etc.) which are all rated on a scale from 1 .. 4. > > I need to present the results as stacked bar charts where the variables > are columns and the percentages of the scales values (1 .. 4) are the > chunks of the stacked bar for each variable. To do this I have transformed > my data from wide to long and calculated the percentage for each variable > and value. The code for this is as follows: > > -- cut -- > > dfr <- structure( > list( > v07_01 = c(3, 1, 1, 4, 3, 4, 4, 1, 3, 2, 2, 3, > 4, 4, 4, 1, 1, 3, 3, 4), > v07_02 = c(1, 2, 1, 1, 2, 1, 4, 1, 1, > 4, 4, 1, 4, 4, 1, 3, 2, 3, 3, 1), > v07_03 = c(3, 2, 2, 1, 4, 1, > 2, 3, 3, 1, 4, 2, 3, 1, 4, 1, 4, 2, 2, 3), > v07_04 = c(3, 1, 1, > 4, 2, 4, 4, 2, 2, 2, 4, 1, 2, 1, 3, 1, 2, 4, 1, 4), > v07_05 = c(1, > 2, 2, 2, 4, 4, 1, 1, 4, 4, 2, 1, 2, 1, 4, 1, 2, 4, 1, 4), > v07_06 = c(1, > 2, 1, 2, 1, 1, 3, 4, 3, 2, 2, 3, 3, 2, 4, 2, 3, 1, 4, 3), > v07_07 = c(3, > 2, 3, 3, 1, 1, 3, 3, 4, 4, 1, 3, 1, 3, 2, 4, 1, 2, 3, 4), > v07_08 = c(3, > 2, 1, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 3, 1, 4, 2, 2, 4), > cased_id = structure( > 1:20, > .Label = c( > "1", > "2", > "3", > "4", > "5", > "6", > "7", > "8", > "9", > "10", > "11", > "12", > "13", > "14", > "15", > "16", > "17", > "18", > "19", > "20" > ), > class = "factor" > ) > ), > .Names = c( > "v07_01", > "v07_02", > "v07_03", > "v07_04", > "v07_05", > "v07_06", > "v07_07", > "v07_08", > "cased_id" > ), > row.names = c(NA, -20L), > class = c("tbl_df", "tbl", > "data.frame") > ) > > mdf <- melt(df) > d_result <- mdf %>% > dplyr::group_by(variable) %>% > count(value) > > ggplot( > d_result, > aes(variable, y = n, fill = value)) + > geom_bar(stat = "identity") + > coord_cartesian(ylim = c(0,100)) > > -- cut -- > > Is there an easier way of doing this, i. e. a way without need to > transform the data? > > How can I change the colors for the data points 1 .. 4? > > I tried > > -- cut -- > > d_result, > aes(variable, y = n, fill = value)) + > geom_bar(stat = "identity") + > coord_cartesian(ylim = c(0,100)) + > scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) > > -- cut - > > but this does not work cause I am mixing continuous and descrete values. > > How can I change the colors for the bars? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
G.Maubach at weinwolf.de
2017-Mar-28 15:56 UTC
[R] Antwort: Re: Way to Plot Multiple Variables and Change Color
Hi Richard, many thanks for your reply. Your solution is not exactly what I was looking for. I would like to know how I can change the colors of the stacked bars in my plot and not use the default values. How can this be done? Kind regards Georg Von: "Richard M. Heiberger" <rmh at temple.edu> An: G.Maubach at weinwolf.de, Kopie: r-help <r-help at r-project.org> Datum: 28.03.2017 17:40 Betreff: Re: [R] Way to Plot Multiple Variables and Change Color I think you are looking for the likert function in the HH package.>From ?likertDiverging stacked barcharts for Likert, semantic differential, rating scale data, and population pyramids. This will get you started. Much more fine control is available. See the examples and demo. ## install.packages("HH") ## if not yet on your system. library(HH) AA <- dfr[,-9] labels <- sort(unique(as.vector(data.matrix(AA)))) result.template <- integer(length(labels)) names(result.template) <- labels BB <- apply(AA, 2, function(x, result=result.template) { tx <- table(x) result[names(tx)] <- tx result } ) BB likert(t(BB), ReferenceZero=0, horizontal=FALSE) On Tue, Mar 28, 2017 at 6:05 AM, <G.Maubach at weinwolf.de> wrote:> Hi All, > > in my current project I have to plot a whole bunch of related variables > (item batteries, e.g. How do you rate ... a) Accelaration, b) HorsePower,> c) Color Palette, etc.) which are all rated on a scale from 1 .. 4. > > I need to present the results as stacked bar charts where the variables > are columns and the percentages of the scales values (1 .. 4) are the > chunks of the stacked bar for each variable. To do this I havetransformed> my data from wide to long and calculated the percentage for eachvariable> and value. The code for this is as follows: > > -- cut -- > > dfr <- structure( > list( > v07_01 = c(3, 1, 1, 4, 3, 4, 4, 1, 3, 2, 2, 3, > 4, 4, 4, 1, 1, 3, 3, 4), > v07_02 = c(1, 2, 1, 1, 2, 1, 4, 1, 1, > 4, 4, 1, 4, 4, 1, 3, 2, 3, 3, 1), > v07_03 = c(3, 2, 2, 1, 4, 1, > 2, 3, 3, 1, 4, 2, 3, 1, 4, 1, 4, 2, 2, 3), > v07_04 = c(3, 1, 1, > 4, 2, 4, 4, 2, 2, 2, 4, 1, 2, 1, 3, 1, 2, 4, 1, 4), > v07_05 = c(1, > 2, 2, 2, 4, 4, 1, 1, 4, 4, 2, 1, 2, 1, 4, 1, 2, 4, 1, 4), > v07_06 = c(1, > 2, 1, 2, 1, 1, 3, 4, 3, 2, 2, 3, 3, 2, 4, 2, 3, 1, 4, 3), > v07_07 = c(3, > 2, 3, 3, 1, 1, 3, 3, 4, 4, 1, 3, 1, 3, 2, 4, 1, 2, 3, 4), > v07_08 = c(3, > 2, 1, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 3, 1, 4, 2, 2, 4), > cased_id = structure( > 1:20, > .Label = c( > "1", > "2", > "3", > "4", > "5", > "6", > "7", > "8", > "9", > "10", > "11", > "12", > "13", > "14", > "15", > "16", > "17", > "18", > "19", > "20" > ), > class = "factor" > ) > ), > .Names = c( > "v07_01", > "v07_02", > "v07_03", > "v07_04", > "v07_05", > "v07_06", > "v07_07", > "v07_08", > "cased_id" > ), > row.names = c(NA, -20L), > class = c("tbl_df", "tbl", > "data.frame") > ) > > mdf <- melt(df) > d_result <- mdf %>% > dplyr::group_by(variable) %>% > count(value) > > ggplot( > d_result, > aes(variable, y = n, fill = value)) + > geom_bar(stat = "identity") + > coord_cartesian(ylim = c(0,100)) > > -- cut -- > > Is there an easier way of doing this, i. e. a way without need to > transform the data? > > How can I change the colors for the data points 1 .. 4? > > I tried > > -- cut -- > > d_result, > aes(variable, y = n, fill = value)) + > geom_bar(stat = "identity") + > coord_cartesian(ylim = c(0,100)) + > scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) > > -- cut - > > but this does not work cause I am mixing continuous and descrete values. > > How can I change the colors for the bars? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
G.Maubach at weinwolf.de
2017-Mar-28 16:07 UTC
[R] Antwort: Re: Way to Plot Multiple Variables and Change Color
Hi Ulrik, your answer is very valuable to me. If you do not know what I do, others don't either. So I should definitely adapt my code. The result of your code and my code is the same. Thus, I use your code cause it is better readable. My other question was how I can change the color palette for the stacked bars. Could you give me a hint where I need to look in ggplot2 documentation? Kind regards Georg Von: Ulrik Stervbo <ulrik.stervbo at gmail.com> An: G.Maubach at weinwolf.de, r-help at r-project.org, Datum: 28.03.2017 16:35 Betreff: Re: [R] Way to Plot Multiple Variables and Change Color Hi Georg, I am a little unsure of what you want to do, but maybe this: mdf <- melt(dfr) d_result <- mdf %>% dplyr::group_by(variable, value) %>% summarise(n = n()) ggplot( d_result, aes(variable, y = n, fill = value)) + geom_bar(stat = "identity") HTH Ulrik On Tue, 28 Mar 2017 at 15:11 <G.Maubach at weinwolf.de> wrote: Hi All, in my current project I have to plot a whole bunch of related variables (item batteries, e.g. How do you rate ... a) Accelaration, b) Horse Power, c) Color Palette, etc.) which are all rated on a scale from 1 .. 4. I need to present the results as stacked bar charts where the variables are columns and the percentages of the scales values (1 .. 4) are the chunks of the stacked bar for each variable. To do this I have transformed my data from wide to long and calculated the percentage for each variable and value. The code for this is as follows: -- cut -- dfr <- structure( list( v07_01 = c(3, 1, 1, 4, 3, 4, 4, 1, 3, 2, 2, 3, 4, 4, 4, 1, 1, 3, 3, 4), v07_02 = c(1, 2, 1, 1, 2, 1, 4, 1, 1, 4, 4, 1, 4, 4, 1, 3, 2, 3, 3, 1), v07_03 = c(3, 2, 2, 1, 4, 1, 2, 3, 3, 1, 4, 2, 3, 1, 4, 1, 4, 2, 2, 3), v07_04 = c(3, 1, 1, 4, 2, 4, 4, 2, 2, 2, 4, 1, 2, 1, 3, 1, 2, 4, 1, 4), v07_05 = c(1, 2, 2, 2, 4, 4, 1, 1, 4, 4, 2, 1, 2, 1, 4, 1, 2, 4, 1, 4), v07_06 = c(1, 2, 1, 2, 1, 1, 3, 4, 3, 2, 2, 3, 3, 2, 4, 2, 3, 1, 4, 3), v07_07 = c(3, 2, 3, 3, 1, 1, 3, 3, 4, 4, 1, 3, 1, 3, 2, 4, 1, 2, 3, 4), v07_08 = c(3, 2, 1, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 3, 1, 4, 2, 2, 4), cased_id = structure( 1:20, .Label = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" ), class = "factor" ) ), .Names = c( "v07_01", "v07_02", "v07_03", "v07_04", "v07_05", "v07_06", "v07_07", "v07_08", "cased_id" ), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame") ) mdf <- melt(df) d_result <- mdf %>% dplyr::group_by(variable) %>% count(value) ggplot( d_result, aes(variable, y = n, fill = value)) + geom_bar(stat = "identity") + coord_cartesian(ylim = c(0,100)) -- cut -- Is there an easier way of doing this, i. e. a way without need to transform the data? How can I change the colors for the data points 1 .. 4? I tried -- cut -- d_result, aes(variable, y = n, fill = value)) + geom_bar(stat = "identity") + coord_cartesian(ylim = c(0,100)) + scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) -- cut - but this does not work cause I am mixing continuous and descrete values. How can I change the colors for the bars? Kind regards Georg ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]