How to draw pie of pie or bar of bar charts in R (using ggplot2)? [duplicate] - r

I know it is possible to create such double pie charts in excel like this:
http://chandoo.org/wp/2009/12/02/group-small-slices-in-pie-charts/
but can SPSS or R do this also?

In relation to R:
The answer to the title question is "yes" ... see ?pie
As for the second question, the one in the body - it would be possible but would involve some coding. You'd have to draw two pie charts side by side (which could be managed with two calls to pie) and use segments or arrows (and text if necessary) to do the additional components of the plot.
Here's a rough example:
That required the fig argument of par to get them side-by-side.
(That example required a little fiddling to get right, but it would be possible to write a function to automate the details.)
The main issue I can see would be 'why on earth would you do it?' -- pie charts are a poor way of conveying information of this form. There are alternatives that result in much better ability to distinguish values, and less bias (such as what you get when comparing nearly horizontal vs nearly vertical slices).

Related

plot panel visualization using ggplot2 in R shiny

I am implementing a R shiny with a plot panel implemented by library(ggplot2). If there are 12 plots, the layout looks great. Please check below.
12-plot layout
However, if I increase the plot number to 70, then each plot looks being compressed (pls see below). Is that possible I can keep the size of each plot fixed? Thank you so much!
enter image description here
Is there another way to approach this? For instance, can you group your data by two categorical variables and use on for colouring and the other for facetting? In that way, you may be able to reduce the number of facets, and stick with the larger facet size, while still conveying all relevant information? 70 facet plots is a lot!
Is this more of a QC thing? For QC, I tend to break it into groups by condition as Paul was suggesting. The reason is that within a condition, things should be really similar. Outside a condition, all bets are off. When I do this for genomics data, I tend to use “pairs” customized to my liking.
What don’t you like about the 70 sample display? Simply the change in aspect ratio? IMO, these are the things I don’t like about ggplot. You can make these plots using base R and then place them on a page manually using par or layout. For that matter, you can do the same with ggplot and use ggarrange or a different manual layout function to place the plots. All wrapped in a for or apply of course.
The other things I like to do when I have a LOT of QCs to look through is create a movie. I can use the forward/back buttons and go through a lot quickly. I like the idea of having this in a dashboard, nice one!
you could also try coord_fixed(ratio= ), not sure if that will work with faceting or not
Finally, I have made a movie-like visualization for those 70 plots using the plot_ly function in R package "plotly".

Is it possible to create a pie in pie chart in SPSS or R?

I know it is possible to create such double pie charts in excel like this:
http://chandoo.org/wp/2009/12/02/group-small-slices-in-pie-charts/
but can SPSS or R do this also?
In relation to R:
The answer to the title question is "yes" ... see ?pie
As for the second question, the one in the body - it would be possible but would involve some coding. You'd have to draw two pie charts side by side (which could be managed with two calls to pie) and use segments or arrows (and text if necessary) to do the additional components of the plot.
Here's a rough example:
That required the fig argument of par to get them side-by-side.
(That example required a little fiddling to get right, but it would be possible to write a function to automate the details.)
The main issue I can see would be 'why on earth would you do it?' -- pie charts are a poor way of conveying information of this form. There are alternatives that result in much better ability to distinguish values, and less bias (such as what you get when comparing nearly horizontal vs nearly vertical slices).

visualization - size of circle proportionate to the value of the item

I'm getting familiar with Graphviz and wonder if it's doable to generate a diagram/graph like the one below (not sure what you call it). If not, does anyone know what's a good open source framework that does it? (pref, C++, Java or Python).
According to Many Eyes‌​, this is a bubble chart. They say:
It is especially useful for data sets with dozens to hundreds of values, or with values that differ by several orders of magnitude.
...
To see the exact value of a circle on the chart, move your mouse over it. If you are charting more than one dimension, use the menu to choose which dimension to show. If your data set has multiple numeric columns, you can choose which column to base the circle sizes on by using the menu at the bottom of the chart.
Thus, any presentation with a lot of bubbles in it (especially with many small bubbles) would have to be dynamic to respond to the mouse.
My usual practice with bubble charts is to show three or four variables (x, y and another variable through the size of the bubble, and perhaps another variable with the color or shading of the bubble). With animation, you can show development over time too - see GapMinder. FlowingData provides a good example with a tutorial on how to make static bubble charts in R.
In the example shown in the question, though, the bubbles appear to be located somewhat to have similar companies close together. Even then, the exact design criteria are unclear to me. For example, I'd have expected Volkswagen to be closer to General Motors than Pfizer is (if some measure of company similarity is used to place the bubbles), but that isn't so in this diagram.
You could use Graphviz to produce a static version of a bubble chart, but there would be quite a lot of work involved to do so. You would have to preprocess the data to calculate a similarity matrix, obtain edge weights from that matrix, assign colours and sizes to each bubble and then have the preprocessing script write the Graphviz file with all edges hidden and run the Graphviz file through neato to draw it.

Can R create a barplot image with clickable bars to insert on a webpage?

I know how to create a barplot, and how to stick it on a webpage; e.g, using hwriteImage in the hwriter package.
What I'd like is for each bar to be a region which highlights on mouseover, and where each bar has a different link when clicked. Similar to this map of the U.S. using the jQuery maphilight plugin, but for a barplot rather than a map. I imagine R could calculate the coordinates of the regions around each bar, generate the HTML AREA tag etc and pass this to maphilight quite easily. Has it been done already? I searched but no luck so far.
Have a look here, which summarises a couple of methods: rggobi and iplots. rggobi looks pretty promising, though maybe the installation looks a bit involved. iplots is only good for scatter plots.
Some other options (I think these are strongest ones at the moment):
googleVis
The googleVis package interfaces with the google charts API: try demo(googleVis) and the third & fourth one are bar chart (there could be more). It has the advantage of being pretty simple to get started with, although these are not R graphics:
df=data.frame(country=c("US", "GB", "BR"), val1=c(10,13,14), val2=c(23,12,32))
Column <- gvisColumnChart(df)
plot(Column)
gridSVG
The gridSVG exports the current grid graphics to an .svg file that can be included into a webpage. Unlike googleVis, it's R graphics (so you can use grid/ggplot2 which are more familiar). It looks like you may have to know some Javascript to further embellish your plots though (e.g. to animate on mouse over, you use grid.garnish(...,onmouseover=...)).
There's some example code you can try here (The really awesome ones are here - usually clicking on the "SVG file" link will have the full interactivity/animation.) (This one is a scatterplot where the points highlight when you move your mouse over them).
As I said - have a look at the package pages, demos, examples, etc to see which suits you.

R: using hatched fill in plots

I was using R to make some plots for a report. I see that the plots don't seem to be so smooth. I am new to R so don't know much. But how do I get smooth plots?
Also by default the plots are filled with solid colors but I want to have hatched fills in my pie charts and bar plots. Is there a way to do that in R, I couldn't find it through a basic google search so I put this question here.
Did you try help(pie)?
density: the density of shading lines, in lines per inch. The default
value of ‘NULL’ means that no shading lines are drawn.
Non-positive values of ‘density’ also inhibit the drawing of
shading lines.
pie(c(1,2,3),density=c(1,2,20))
hist(runif(200),density=c(10,20,30))
Note that cross hatching and filling with lines is a hold over from the days when the only way to get quality graphics was with pen plotters (devices that literally drew the plot using a mechanical arm and a pen). Solid fills did not work well on those devices, often making holes in the paper when trying.
Now with high quality monitors and printers, solid fills are prefered, in fact research has shown that cross hatching can cause optical illusions due to the Moire effect, so are best avoided.
Pie charts are also best avoided, the only thing they do better than bar charts is hide and obscure data. Dotplots are becoming prefered to bar charts and with dotplots you don't need to worry about fills.
I know this does not answer the question you asked, but it does answer the one you should have asked.
The idea that hatching should be consigned to the history books is all well and good, but there are still situations where you have to use hatching rather than solid fills - i.e. when you have to present something in black and white. Many academic journals, for instance, still only have black and white figures so using solid fills isn't an option if you want to do a stack chart with more than a few options.

Resources