[R] Way to Plot Multiple Variables and Change Color

Richard M. Heiberger rmh at temple.edu
Tue Mar 28 18:04:47 CEST 2017


The colors can be specified with the standard lattice "col=" argument.

likert(t(BB), ReferenceZero=0, horizontal=FALSE,
       col=RColorBrewer::brewer.pal(4, "Blues"))

Most other customizations you might need are also possible.  Many examples
are in the examples section of ?likert and in the demo.

Rich


On Tue, Mar 28, 2017 at 8:56 AM,  <G.Maubach at weinwolf.de> wrote:
> 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 ?likert
>
>
> Diverging 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.
>
>



More information about the R-help mailing list