ggplot2: Impossible to create stacked bar chart WITHOUT reshape/melt? - r

I am a novice at R and experimenting with as an alternative for data visualisation.
I am having trouble creating a stacked bar chart.
I have tried the reshape2 package with the melt function and have successfully produced one, but I had to explicitly create a dataset containing JUST the x-axis and variables that I want stacked.
It seems extremely counter-intuitive to me that we can't visualise data from a left to right sense (x-axis constant, y variables summed and overlapping).
Is there an alternate method, where I could simply perform a ggplot with the logic of:
ggplot(data=dataset, aes(x=Time, y1=var1, y2=var2, y3=var3.....)) +
geom_bar(stat="identity",position="stack")
where y1, y2, y3 are the variables I want stacked, but do not have corresponding flags for me to use a "fill=flag" type?
I basically want to work off one large master dataset and export multiple analysis without having to excessively isolate each dataset and melt it

In general a stacked bar chart is used to distinguish between variations within a single category of data. For example if you had a bar chart showing the population of three species of migratory fowl that inhabit one specific marsh.
The bars might be mallard ducks, muted swans & Canada geese. Each would have a single whole bar.
The stacking would come in when you looked at these with a trait or quality they might share which you were comparing, such as the number who migrate and those who overwinter locally. The population of each type of fowl would be split into two stacks in the bar, those migrating who are Canada geese, those not...and so on.
It is not really meant to bring together disparate traits into a stack.
So, if you have data that separates out categories of the same population, reshaping the data to create a set of individual types within your data in columns, then differentiating by factors in another (also all in the same column) that is the right move.
If you need to keep it extracted for some reason, you can probably use y = (x$1 +x$2 x$b) to create your stacks, but depending on the data that might fail miserably. The best thing to do is reshape so that the quality you are counting is in a column and you compare those members across some other column with stacks.
If you need to use the data in another format later, create a temporary table, plot and then remove() it and gc() after graphing to get your memory back

Related

Issues with combining different (continuous and ordinal) plot types into one plot

I am preparing a figure for a paper presenting data for 2 different experiments in one plot. For that reason I don't need a legend for every plot, so I try to combine them with ggdraw from cowplot.
My code
should generate a reproducible example
and gives this output:
It seems like the two figures get the same slot (A) and the legend gets slot (B). Typically, I would probably use facet wrap to plot them together (which should also guarantee that the scaling/legend is consistent across the two plots.), but that will probably not work in this case, as I am trying to add an additional figure type to C and D.
The problem is that this figure type is ordinal so I have used a somewhat “hacky” approach to plot it, giving me this figure looking essentially as I want it to:
I so far have not been able to extract to another element that ggdraw can use.
Ideally the final plot should roughly look like this (of course with different labels):
How would you go about plotting these different types together?
Thank you for taking time to read my question and I hope that you can help me. I now it is quite a mouth full, but I was not sure how I meaningfully could reduce it to smaller chunks.

How to combine two data sets into one and plotting one graph with both data sets in R Studio

Currently I have combined the Apple stock market and the Samsung stock market from 2014- 2018. I have combined the Date,Open,High, low and Close using cbind and changed the names so it says Apple/ Samsung.
My problem is with the graph, Now my dataset is combined in columns so I feel like this might be part of the problem, but none the less I would prefer to keep it like that. I would love a graph that would have both of the open figures on it over the years.
If I just use plot(Total$OpenApple, Total$OpenSam) the plot is a huge block compared to the line graph I would like.
Thanks.
Without any example data its difficult to understand your problem fully. However, I would try using the ggplot2 package and dplyr package. Then you can change your data so that OpenApple and OpenSam are both part of the same column, and then use a function from ggplot2 to change the colors of your lines based on what group they are a part of.

Making a 'flip-book' type animation using density plots from R

I'm new to R, but have worked out how to graph the distribution of my students' grades for a given term using a density plot, and have made some ridgeline plots to show how the distribution evolves throughout the academic year.
I'm thinking it might be fun (and make the graphs easier to interpret) if I could make a kind of flip-book animation that went from one terms grades to the next, relatively quickly, to see how the distribution changes. At its simplest, I could just pop these distribution plots into Powerpoint and just scroll through the pages, but I'm wondering what commands I need to put into R's ggplot command to ensure that the axes/scaling from one chart to the next stays consistent from one chart to the next?
At the moment, I'm just making a simple chart using this command, where HT102 is the data from the 2nd term of Year 10, and A8 is a vector containing all the (numeric) grades. I am then doing the same thing with another set of grades called ht103, and so on...
ggplot(ht102, aes(x = A8)) +
geom_density(alpha=.3)
What would you recommend to keep the scaling consistent, and any thoughts on a better way to animate this than just popping them into powerpoint?

Power View - Tables with columns of the similar data

Apologies in advance if my question is obvious but I'm new to this and having spent days searching and experimenting I cannot achieve the result I'm after.
I have a big table of data that I want to plot some graphs for, namely stacked columns and then filter these using other criteria such as date. I came across Power View and have been learning to use it hoping it will let me produce the reports I'm after rather than standard Excel graphs which are very clunky.
An example of the sort of data my tables contains is as follows although I have a lot of other columns and about 20 of the similar metric columns:
]
And this is the sort of graph I want to plot:
]
Where the red sections correspond to "R0" values, the orange "R1" and the green "R2" - they're essentially a rating; poor, ok and good. I can plot a single metric versus the items column with the stacked bar fine but cannot find a way to plot the metrics long the x axis for say a given item or sum of all items.
I've created measures using CALCULATE to filter by the rating but when I try and plot these my only option in power view is a clustered column graph where the x axis is the rating and the legend is the metrics.
I also created another small table with a single column of R0, R1 and R2 but can only link that to one metric column whereas I need it to link to all of them.
I think it's potentially a many to many mapping issue and have found a lot of links covering bridge tables and the magic CALCULATE function.
However as I'm trying to map values in a column to several other columns it doesn't seem to quite fit the many to many problem or if it does I can't see it.
I feel like what I'm after should be quite simple but I either end up with all my metric columns being made to show identical values or there's loads of cross filtering that I don't want. The "ratings" for each metric are essentially independent and I don't want to combine them in any way.
Any help is greatly appreciated and if my solution is in the links I've listed above then I'd really appreciate a bit of help with seeing it.
Thanks in advance

Cross-Recurrence plots in R (with or without ggplot)

I have different time-series corresponding to different individuals and their location within a building (a categorical variable -- more like a room name).
I would like to study the similarity in movement of different individuals by something like cross-recurrence plots, where the two time-series correspond to the two axes and the actual points correspond to the presence/absence of individuals in the same room.
Has anyone tried doing such plots in R or while using ggplot? Any help would be great!
I haven't used this routine. I used only d2 dimension and Lyapunov exponent for EEG but this package Tisean (RTisean for your case) has a routine ['recurr'] that returns the specific plot.
This link has a nice wrap up of tutorials and links
Edited:
In this link you can find a nice example of application of recurrence plot.
The return variables of function recur(and similar functions of other packages) you can access after putting $ after the dataset (like database)
and you can access them inside in ggplot function and applying the appropriate aes.

Resources