G.Maubach at weinwolf.de
2017-Apr-10 14:45 UTC
[R] Antwort: Re: Antwort: Re: Way to Plot Multiple Variables and Change Color
Hi Ulrik,
many thanks for your reply. I had to take an unplanned break and was not
in the office during the last two weeks. Thus my late reply.
I followed your advice and converted the variable in argument "fill"
to
factor. Now the color change works:
-- cut --
d_result <- structure(list("variable" = c("Item 1 (? = 3.3)
", "Item 1 (?
= 3.3) ",
"Item 1 (? = 3.3) ",
"Item 1 (? =
3.3) ", "Item 1 (? = 3.3) ",
"Item 1 (? = 3.3) ",
"Item 2 (? =
3.8) ", "Item 2 (? = 3.8) ",
"Item 2 (? = 3.8) ",
"Item 2 (? =
3.8) ", "Item 2 (? = 3.8) ",
"Item 2 (? = 3.8) ",
"Item 3 (? =
3.4) ", "Item 3 (? = 3.4) ",
"Item 3 (? = 3.4) ",
"Item 3 (? =
3.4) ", "Item 3 (? = 3.4) ",
"Item 3 (? = 3.4) ",
"Item 4 (? =
3.4) ", "Item 4 (? = 3.4) ",
"Item 4 (? = 3.4) ",
"Item 4 (? =
3.4) ", "Item 4 (? = 3.4) ",
"Item 4 (? = 3.4) ",
"Item 5 (? =
3.5) ", "Item 5 (? = 3.5) ",
"Item 5 (? = 3.5) ",
"Item 5 (? =
3.5) ", "Item 5 (? = 3.5) ",
"Item 5 (? = 3.5) ",
"Item 6 (? =
3.5) ", "Item 6 (? = 3.5) ",
"Item 6 (? = 3.5) ",
"Item 6 (? =
3.5) ", "Item 6 (? = 3.5) ",
"Item 6 (? = 3.5) ",
"Item 7 (? =
3.4) ", "Item 7 (? = 3.4) ",
"Item 7 (? = 3.4) ",
"Item 7 (? =
3.4) ", "Item 7 (? = 3.4) ",
"Item 7 (? = 3.4) ",
"Item 8 (? =
3.3) ", "Item 8 (? = 3.3) ",
"Item 8 (? = 3.3) ",
"Item 8 (? =
3.3) ", "Item 8 (? = 3.3) ",
"Item 8 (? = 3.3) "), value =
structure(c(1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1 = very
satisfied",
"2",
"3",
"4", "5", "6 = very dissatified"), class =
"factor"),
n = c(14L, 20L, 24L, 14L, 16L, 14L, 9L, 15L,
21L, 20L, 14L,
23L, 19L, 17L, 16L, 14L, 16L, 20L, 22L,
17L, 15L, 16L, 20L,
12L, 19L, 15L, 16L, 15L, 18L, 19L, 18L,
15L, 18L, 18L, 16L,
17L, 17L, 20L, 17L, 17L, 14L, 16L, 16L,
25L, 16L, 17L, 8L,
20L)), .Names = c("variable",
"value",
"n"), row.names c(NA,
-48L), vars = list("variable"), drop = TRUE,
indices list(0:5,
6:11, 12:17, 18:23, 24:29, 30:35, 36:41,
42:47),
group_sizes = c(6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L),
biggest_group_size = 6L,
labels = structure(list(
"variable" = structure(1:8, .Label =
c("Item 1 (?
= 3.3) ",
"Item 2 (? =
3.8) ", "Item 3 (? = 3.4) ", "Item 4 (? = 3.4) ",
"Item 5 (? =
3.5) ", "Item 6 (? = 3.5) ", "Item 7 (? = 3.4) ",
"Item 8 (? =
3.3) "), class = "factor")),
row.names = c(NA,
-8L), class = "data.frame", vars
=
list("variable"),
drop = TRUE, .Names = "variable"),
class = c("grouped_df",
"tbl_df", "tbl",
"data.frame"))
ggplot(
d_result,
aes(x = variable, y = n, fill = rev(factor(value)))) +
geom_bar(
stat = "identity") +
coord_cartesian(ylim = c(0,100)) +
coord_flip() +
scale_y_continuous(name = "Percent") +
scale_fill_manual(
values = rev(
c(
"forestgreen", "limegreen",
"gold", "orange1",
"tomato3", "darkred"))) +
ggtitle(
paste(
"Question 8: Satisfaction?")) +
labs(fill = "Rating") +
scale_x_discrete(
name = element_blank()) +
# scale_color_manual(
# values = rev(
# c(
# "forestgreen", "limegreen",
# "gold", "orange1",
# "tomato3", "darkred"))) +
geom_text(
aes(label = n),
color = "white",
position = position_stack(vjust = 0.5)) +
theme_minimal() +
theme(
legend.position = "right")
-- cut --
I tried to change the order of the items on the y-axis, e.g. Item 8
should be last and Item 1 first. I tried to reverse the order of the items
within ggplot using rev() and relevel(). But neither of them worked. Is
there a way to do it?
I also tried to adjust the color palette for the legend, e.g. 1 = very
satisfied is green, 6 = very dissatified is red instead of vice versa as
it is now. The result should ensure the item naming for 1 = satisfied and
6 = unsatifies cause this is the way it was asked in the questionnaire.
Thus my question is:
1. How can I change the order of the sequence for the y-axis?
2. How can I adjust the color palette of the legend that it matches the
correct items?
Can you give me a hint which functions I could use to do it?
Kind regards
Georg
Von: Ulrik Stervbo <ulrik.stervbo at gmail.com>
An: G.Maubach at weinwolf.de, "Richard M. Heiberger" <rmh at
temple.edu>,
Kopie: r-help <r-help at r-project.org>
Datum: 28.03.2017 18:32
Betreff: Re: [R] Antwort: Re: Way to Plot Multiple Variables and
Change Color
Hi Georg,
you were on the right path - it is all about scale_fill*
The 'problem' as you've discovered is that value is continuous, but
applying scale_fill_manual or others (except scale_fill_gradient) expects
discrete values.
The solution is simply to set the fill with that by using factor():
ggplot(
d_result,
aes(variable, y = n, fill = factor(value))) +
geom_bar(stat = "identity") +
scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues"))
or:
ggplot(
d_result,
aes(variable, y = n, fill = factor(value))) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("red","blue",
"green", "purple"))
When using colorBrewer (which I highly recommend), I use scale_*_brewer
rather than setting the colour manually:
ggplot(
d_result,
aes(variable, y = n, fill = factor(value))) +
geom_bar(stat = "identity") +
scale_fill_brewer(palette = "Blues ")
Best,
Ulrik
On Tue, 28 Mar 2017 at 18:21 <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.
______________________________________________
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.
David Winsemius
2017-Apr-10 20:06 UTC
[R] Antwort: Re: Antwort: Re: Way to Plot Multiple Variables and Change Color
> On Apr 10, 2017, at 7:45 AM, G.Maubach at weinwolf.de wrote: > > Hi Ulrik, > > many thanks for your reply. I had to take an unplanned break and was not > in the office during the last two weeks. Thus my late reply. > > I followed your advice and converted the variable in argument "fill" to > factor. Now the color change works: > > -- cut -- > > d_result <- structure(list("variable" = c("Item 1 (? = 3.3) ", "Item 1 (? > = 3.3) ", > "Item 1 (? = 3.3) ", "Item 1 (? = > 3.3) ", "Item 1 (? = 3.3) ", > "Item 1 (? = 3.3) ", "Item 2 (? = > 3.8) ", "Item 2 (? = 3.8) ", > "Item 2 (? = 3.8) ", "Item 2 (? = > 3.8) ", "Item 2 (? = 3.8) ", > "Item 2 (? = 3.8) ", "Item 3 (? = > 3.4) ", "Item 3 (? = 3.4) ", > "Item 3 (? = 3.4) ", "Item 3 (? = > 3.4) ", "Item 3 (? = 3.4) ", > "Item 3 (? = 3.4) ", "Item 4 (? = > 3.4) ", "Item 4 (? = 3.4) ", > "Item 4 (? = 3.4) ", "Item 4 (? = > 3.4) ", "Item 4 (? = 3.4) ", > "Item 4 (? = 3.4) ", "Item 5 (? = > 3.5) ", "Item 5 (? = 3.5) ", > "Item 5 (? = 3.5) ", "Item 5 (? = > 3.5) ", "Item 5 (? = 3.5) ", > "Item 5 (? = 3.5) ", "Item 6 (? = > 3.5) ", "Item 6 (? = 3.5) ", > "Item 6 (? = 3.5) ", "Item 6 (? = > 3.5) ", "Item 6 (? = 3.5) ", > "Item 6 (? = 3.5) ", "Item 7 (? = > 3.4) ", "Item 7 (? = 3.4) ", > "Item 7 (? = 3.4) ", "Item 7 (? = > 3.4) ", "Item 7 (? = 3.4) ", > "Item 7 (? = 3.4) ", "Item 8 (? = > 3.3) ", "Item 8 (? = 3.3) ", > "Item 8 (? = 3.3) ", "Item 8 (? = > 3.3) ", "Item 8 (? = 3.3) ", > "Item 8 (? = 3.3) "), value = > structure(c(1L, 2L, 3L, 4L, 5L, > 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, > 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, > 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1 = very > satisfied", > "2", "3", > "4", "5", "6 = very dissatified"), class = "factor"), > n = c(14L, 20L, 24L, 14L, 16L, 14L, 9L, 15L, > 21L, 20L, 14L, > 23L, 19L, 17L, 16L, 14L, 16L, 20L, 22L, > 17L, 15L, 16L, 20L, > 12L, 19L, 15L, 16L, 15L, 18L, 19L, 18L, > 15L, 18L, 18L, 16L, > 17L, 17L, 20L, 17L, 17L, 14L, 16L, 16L, > 25L, 16L, 17L, 8L, > 20L)), .Names = c("variable", "value", > "n"), row.names > c(NA, > -48L), vars = list("variable"), drop = TRUE, > indices > list(0:5, > 6:11, 12:17, 18:23, 24:29, 30:35, 36:41, > 42:47), > group_sizes = c(6L, > 6L, 6L, 6L, 6L, 6L, 6L, 6L), > biggest_group_size = 6L, > labels = structure(list( > "variable" = structure(1:8, .Label = c("Item 1 (? > = 3.3) ", > "Item 2 (? = > 3.8) ", "Item 3 (? = 3.4) ", "Item 4 (? = 3.4) ", > "Item 5 (? = > 3.5) ", "Item 6 (? = 3.5) ", "Item 7 (? = 3.4) ", > "Item 8 (? = > 3.3) "), class = "factor")), > row.names = c(NA, > -8L), class = "data.frame", vars = > list("variable"), > drop = TRUE, .Names = "variable"), > class = c("grouped_df", > "tbl_df", "tbl", "data.frame")) > > ggplot( > d_result, > aes(x = variable, y = n, fill = rev(factor(value)))) + > geom_bar( > stat = "identity") + > coord_cartesian(ylim = c(0,100)) + > coord_flip() + > scale_y_continuous(name = "Percent") + > scale_fill_manual( > values = rev( > c( > "forestgreen", "limegreen", > "gold", "orange1", > "tomato3", "darkred"))) + > ggtitle( > paste( > "Question 8: Satisfaction?")) + > labs(fill = "Rating") + > scale_x_discrete( > name = element_blank()) + > # scale_color_manual( > # values = rev( > # c( > # "forestgreen", "limegreen", > # "gold", "orange1", > # "tomato3", "darkred"))) + > geom_text( > aes(label = n), > color = "white", > position = position_stack(vjust = 0.5)) + > theme_minimal() + > theme( > legend.position = "right") > > -- cut -- > > I tried to change the order of the items on the y-axis, e.g. Item 8 > should be last and Item 1 first."First" and "last" apparently mean "top" and "bottom" to you. Since the $variable column is character, and ordering is typically done by setting levels of factors, try: d_result$variable <- factor(d_result$variable, levels=rev(unique(d_result$variable))) # changes ordering so the "Item 1"'s are at the top.> I tried to reverse the order of the items > within ggplot using rev() and relevel(). But neither of them worked. Is > there a way to do it?I don't think you can relevel a character column.> > I also tried to adjust the color palette for the legend, e.g. 1 = very > satisfied is green, 6 = very dissatified is red instead of vice versa as > it is now. The result should ensure the item naming for 1 = satisfied and > 6 = unsatifies cause this is the way it was asked in the questionnaire. > > Thus my question is: > > 1. How can I change the order of the sequence for the y-axis? > > 2. How can I adjust the color palette of the legend that it matches the > correct items?You probably could use relevel sinc `value` was a factor but I found it easier to simply repeat the relevelling code and change the target column name: d_result$value <- factor(d_result$value, levels=rev(unique(d_result$value))) I did find the appearance of the final result stange because there was irregular use of "\n" in the "variable" character values. that created more items than I think you wanted to appear. HTH; David.> > Can you give me a hint which functions I could use to do it? > > Kind regards > > Georg > > > > > Von: Ulrik Stervbo <ulrik.stervbo at gmail.com> > An: G.Maubach at weinwolf.de, "Richard M. Heiberger" <rmh at temple.edu>, > Kopie: r-help <r-help at r-project.org> > Datum: 28.03.2017 18:32 > Betreff: Re: [R] Antwort: Re: Way to Plot Multiple Variables and > Change Color > > > > Hi Georg, > > you were on the right path - it is all about scale_fill* > > The 'problem' as you've discovered is that value is continuous, but > applying scale_fill_manual or others (except scale_fill_gradient) expects > discrete values. > > The solution is simply to set the fill with that by using factor(): > > ggplot( > d_result, > aes(variable, y = n, fill = factor(value))) + > geom_bar(stat = "identity") + > scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) > or: > ggplot( > d_result, > aes(variable, y = n, fill = factor(value))) + > geom_bar(stat = "identity") + > scale_fill_manual(values = c("red","blue", "green", "purple")) > > When using colorBrewer (which I highly recommend), I use scale_*_brewer > rather than setting the colour manually: > > ggplot( > d_result, > aes(variable, y = n, fill = factor(value))) + > geom_bar(stat = "identity") + > scale_fill_brewer(palette = "Blues ") > > Best, > Ulrik > > > On Tue, 28 Mar 2017 at 18:21 <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. > > ______________________________________________ > 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. > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA
David Winsemius
2017-Apr-10 20:21 UTC
[R] Antwort: Re: Antwort: Re: Way to Plot Multiple Variables and Change Color
> On Apr 10, 2017, at 1:06 PM, David Winsemius <dwinsemius at comcast.net> wrote: > > >> On Apr 10, 2017, at 7:45 AM, G.Maubach at weinwolf.de wrote: >> >> Hi Ulrik, >> >> many thanks for your reply. I had to take an unplanned break and was not >> in the office during the last two weeks. Thus my late reply. >> >> I followed your advice and converted the variable in argument "fill" to >> factor. Now the color change works: >> >> -- cut -- >> >> d_result <- structure(list("variable" = c("Item 1 (? = 3.3) ", "Item 1 (? = 3.3) ", >> "Item 1 (? = 3.3) ", "Item 1 (? = 3.3) ", "Item 1 (? = 3.3) ", >> "Item 1 (? = 3.3) ", "Item 2 (? = 3.8) ", "Item 2 (? = 3.8) ", >> "Item 2 (? = 3.8) ", "Item 2 (? = 3.8) ", "Item 2 (? = 3.8) ", >> "Item 2 (? = 3.8) ", "Item 3 (? = 3.4) ", "Item 3 (? = 3.4) ", >> "Item 3 (? = 3.4) ", "Item 3 (? = 3.4) ", "Item 3 (? = 3.4) ", >> "Item 3 (? = 3.4) ", "Item 4 (? = 3.4) ", "Item 4 (? = 3.4) ", >> "Item 4 (? = 3.4) ", "Item 4 (? = 3.4) ", "Item 4 (? = 3.4) ", >> "Item 4 (? = 3.4) ", "Item 5 (? = 3.5) ", "Item 5 (? = 3.5) ", >> "Item 5 (? = 3.5) ", "Item 5 (? = 3.5) ", "Item 5 (? = 3.5) ", >> "Item 5 (? = 3.5) ", "Item 6 (? = 3.5) ", "Item 6 (? = 3.5) ", >> "Item 6 (? = 3.5) ", "Item 6 (? = 3.5) ", "Item 6 (? = 3.5) ", >> "Item 6 (? = 3.5) ", "Item 7 (? = 3.4) ", "Item 7 (? = 3.4) ", >> "Item 7 (? = 3.4) ", "Item 7 (? = 3.4) ", "Item 7 (? = 3.4) ", >> "Item 7 (? = 3.4) ", "Item 8 (? = 3.3) ", "Item 8 (? = 3.3) ", >> "Item 8 (? = 3.3) ", "Item 8 (? = 3.3) ", "Item 8 (? = 3.3) ", >> "Item 8 (? = 3.3) "), value = >> structure(c(1L, 2L, 3L, 4L, 5L, >> 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, >> 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, >> 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1 = very >> satisfied", >> "2", "3", >> "4", "5", "6 = very dissatified"), class = "factor"), >> n = c(14L, 20L, 24L, 14L, 16L, 14L, 9L, 15L, >> 21L, 20L, 14L, >> 23L, 19L, 17L, 16L, 14L, 16L, 20L, 22L, >> 17L, 15L, 16L, 20L, >> 12L, 19L, 15L, 16L, 15L, 18L, 19L, 18L, >> 15L, 18L, 18L, 16L, >> 17L, 17L, 20L, 17L, 17L, 14L, 16L, 16L, >> 25L, 16L, 17L, 8L, >> 20L)), .Names = c("variable", "value", >> "n"), row.names >> c(NA, >> -48L), vars = list("variable"), drop = TRUE, >> indices >> list(0:5, >> 6:11, 12:17, 18:23, 24:29, 30:35, 36:41, >> 42:47), >> group_sizes = c(6L, >> 6L, 6L, 6L, 6L, 6L, 6L, 6L), >> biggest_group_size = 6L, >> labels = structure(list( >> "variable" = structure(1:8, .Label = c("Item 1 (? >> = 3.3) ", >> "Item 2 (? = >> 3.8) ", "Item 3 (? = 3.4) ", "Item 4 (? = 3.4) ", >> "Item 5 (? = >> 3.5) ", "Item 6 (? = 3.5) ", "Item 7 (? = 3.4) ", >> "Item 8 (? = >> 3.3) "), class = "factor")), >> row.names = c(NA, >> -8L), class = "data.frame", vars = >> list("variable"), >> drop = TRUE, .Names = "variable"), >> class = c("grouped_df", >> "tbl_df", "tbl", "data.frame")) >> >> ggplot( >> d_result, >> aes(x = variable, y = n, fill = rev(factor(value)))) + >> geom_bar( >> stat = "identity") + >> coord_cartesian(ylim = c(0,100)) + >> coord_flip() + >> scale_y_continuous(name = "Percent") + >> scale_fill_manual( >> values = rev( >> c( >> "forestgreen", "limegreen", >> "gold", "orange1", >> "tomato3", "darkred"))) + >> ggtitle( >> paste( >> "Question 8: Satisfaction?")) + >> labs(fill = "Rating") + >> scale_x_discrete( >> name = element_blank()) + >> # scale_color_manual( >> # values = rev( >> # c( >> # "forestgreen", "limegreen", >> # "gold", "orange1", >> # "tomato3", "darkred"))) + >> geom_text( >> aes(label = n), >> color = "white", >> position = position_stack(vjust = 0.5)) + >> theme_minimal() + >> theme( >> legend.position = "right") >> >> -- cut -- >> >> I tried to change the order of the items on the y-axis, e.g. Item 8 >> should be last and Item 1 first. > > "First" and "last" apparently mean "top" and "bottom" to you. Since the $variable column is character, and ordering is typically done by setting levels of factors, try: > > d_result$variable <- factor(d_result$variable, levels=rev(unique(d_result$variable))) > > > # changes ordering so the "Item 1"'s are at the top. > > >> I tried to reverse the order of the items >> within ggplot using rev() and relevel(). But neither of them worked. Is >> there a way to do it? > > I don't think you can relevel a character column. >> >> I also tried to adjust the color palette for the legend, e.g. 1 = very >> satisfied is green, 6 = very dissatified is red instead of vice versa as >> it is now. The result should ensure the item naming for 1 = satisfied and >> 6 = unsatifies cause this is the way it was asked in the questionnaire. >> >> Thus my question is: >> >> 1. How can I change the order of the sequence for the y-axis? >> >> 2. How can I adjust the color palette of the legend that it matches the >> correct items? > > You probably could use relevel sinc `value` was a factor but I found it easier to simply repeat the relevelling code and change the target column name: > > d_result$value <- factor(d_result$value, levels=rev(unique(d_result$value))) > > I did find the appearance of the final result stange because there was irregular use of "\n" in the "variable" character values. that created more items than I think you wanted to appear.I edited the code above to take out the inadvertent linefeeds (which got inserted by some part of the mail-processing chain) and then ran the ggplot call inside pdf() and print(...): -------------- next part -------------- A non-text attachment was scrubbed... Name: Rplots.pdf Type: application/pdf Size: 5539 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170410/346c3c9b/attachment.pdf> -------------- next part -------------- HTH; David.>> >> Can you give me a hint which functions I could use to do it? >> >> Kind regards >> >> Georg >> >> >> >> >> Von: Ulrik Stervbo <ulrik.stervbo at gmail.com> >> An: G.Maubach at weinwolf.de, "Richard M. Heiberger" <rmh at temple.edu>, >> Kopie: r-help <r-help at r-project.org> >> Datum: 28.03.2017 18:32 >> Betreff: Re: [R] Antwort: Re: Way to Plot Multiple Variables and >> Change Color >> >> >> >> Hi Georg, >> >> you were on the right path - it is all about scale_fill* >> >> The 'problem' as you've discovered is that value is continuous, but >> applying scale_fill_manual or others (except scale_fill_gradient) expects >> discrete values. >> >> The solution is simply to set the fill with that by using factor(): >> >> ggplot( >> d_result, >> aes(variable, y = n, fill = factor(value))) + >> geom_bar(stat = "identity") + >> scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Blues")) >> or: >> ggplot( >> d_result, >> aes(variable, y = n, fill = factor(value))) + >> geom_bar(stat = "identity") + >> scale_fill_manual(values = c("red","blue", "green", "purple")) >> >> When using colorBrewer (which I highly recommend), I use scale_*_brewer >> rather than setting the colour manually: >> >> ggplot( >> d_result, >> aes(variable, y = n, fill = factor(value))) + >> geom_bar(stat = "identity") + >> scale_fill_brewer(palette = "Blues ") >> >> Best, >> Ulrik >> >> >> On Tue, 28 Mar 2017 at 18:21 <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. >> >> ______________________________________________ >> 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. >> >> ______________________________________________ >> 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. > > David Winsemius > Alameda, CA, USA > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA