[R] help with stacked ggplot
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Sat Aug 19 08:56:54 CEST 2017
If you look below at the detritus of your posting you will see why we are
always reminding people to adjust their email programs to send PLAIN
TEXT... since this is a plain text mailing list and HTML formatted email
does not come through reliably. This and other ground rules are laid out
in the Posting Guide mentioned in the footer of every email on the mailing
list.
I also recommend looking into the reprex package... it can conveniently
check any code example to confirm that it is self-contained so it should
run on our computers and we can see your progress. The dput function is
also useful for giving us a snippet of data in our R environments (not
much to look at, but makes R quite happy). I am going to pass on trying to
decipher the mess below.
However, I think the key to answering your query is learning how to create
factor columns in your data set (hint: use the factor function, not the
as.factor function). The order of the labels in the factor is preserved in
the plot (assuming the structure of the plot is not too complicated), so
you can use scale_* functions such as scale_fill_manual to control the
corresponding graphical appearance.
On Fri, 18 Aug 2017, Mary Rigdon wrote:
> Hi,
>
> I am new to R and this is probably a very basic question but I can?t seem to figure out a solution. I am creating a stacked ggplot with the following data and code:
>
> PercentageData.csv looks like this:
>
> decision
> treatment
> percentage
> labtreatment
> defect
> 0
> 53.49
> COMMON
> defect
> 1
> 78.00
> ASYMMETRIC
> defect
> 2
> 96.67
> PRIVATE
> coop
> 0
> 46.51
> COMMON
> coop
> 1
> 22.00
> ASYMMETRIC
> coop
> 2
> 3.33
> PRIVATE
>
> %Load data:
>
> charts.data <- read.csv("PercentageData.csv?)
>
> %create stacked ggplot
>
> p4 <- ggplot() + geom_bar(aes(y = percentage, x = treatment, fill = decision), data = charts.data, stat="identity")
>
> p4
>
> %y-axis is percentage and x-axis is treatment, ordered 0, 1, 2
>
> I?d like to have the treatment names be strings so that 0=?COMMON?, 1=?ASYMMETRIC?, 2=?PRIVATE?. How do I assign names to the treatment variable?
>
> I?ve tried to just have a new variable, x=labtreatment, but then the order of my data is graphed alphabetical (and not what I am looking for); Asymmetric on the left, Common in the middle, then Private on the right. Is there an easy way to reorder this?
>
> Thanks for any help!
>
> Best,
> Mary
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
More information about the R-help
mailing list