Attempting to create pie chart with ggplot2 but cannot seem to get it using other references online. The chart I create is missing most of its fill.
ggplot(sae,aes(x=1,fill=factor(State), width=1))+
geom_bar()+
ggtitle("House by State")+
coord_polar(theta='y')
This code gives:
How do I fill the center?
Any other improvements appreciated.
With sample data
sae <- data.frame(State=sample(LETTERS[1:6],60,T))
ggplot(sae,aes(x=factor(1),fill=factor(State)))+
geom_bar(width=1)+
ggtitle("House by State")+
coord_polar(theta="y")
EDIT: Other options (because piecharts are bad)
#following Jaaps example: some better way to visualize this
#grouped barchart
p1 <- ggplot(sae, aes(x=State, fill=State)) +
geom_bar() + labs(title="grouped barchart")
#stacked barchart; especially practical if you want to compare groups
sae$group <- rbinom(60,1,0.5)
p2 <- ggplot(sae, aes(x=factor(group),fill=State))+
geom_bar(width=0.5) + labs(title="grouped stacked barchart")
do.call(grid.arrange,list(grobs=list(p1,p2),ncol=2))
As #Heroka already mentioned in the comments, pie-charts are a bad way of visualizing information. They are bad that it is even mentioned in the help-files of R.
From ?pie:
Pie charts are a very bad way of displaying information. The eye is
good at judging linear measures and bad at judging relative areas. A
bar chart or dot chart is a preferable way of displaying this type of
data.
Cleveland (1985), page 264: “Data that can be shown by pie charts
always can be shown by a dot chart. This means that judgements of
position along a common scale can be made instead of the less accurate
angle judgements.” This statement is based on the empirical
investigations of Cleveland and McGill as well as investigations by
perceptual psychologists.
Some further reading on the pie-chart debate.
With the example data of #Heroka:
ggplot(sae,aes(x = factor(1), fill = factor(State)))+
geom_bar(width = 1, position = "dodge")+
ggtitle("House by State")
you get:
A clear demonstration that it's better to see the differences between the categories when you use a barchart instead of a piechart.
When you want to show information about proportions, there is another choice, the waffle package which gets back more to what you probably intend to show with a pie chart (i.e., proportions). In most instances, the bar plots above would likely be best, but for the sake of showing another way of plotting...
Using the sae data from above:
library(waffle) # install the package if you don't have it
w <- table(sae)
w.waf <- waffle(table(sae))
w.waf + ggtitle("Contextless Waffle Graph") + theme(plot.title=element_text(face="bold", size=24))
which yields this:
Related
I have a question about facet_wrap() in ggplot2.
I am trying to make a graph that looks like this. I attach an example image 1.enter image description here
In image 1 it can be seen that there are two maps and each one has its legend and color scale. I would like to be able to do this with ggplot and the facet_wrap() function.
My problem is that because the data in the dataframe is very different, they have a lot of amplitude for each map, when plotting the scale it does not allow me to visualize it the way I want.
enter image description here
ggplot(dataframe,mapping=aes(x=lon,x=lat))+
geom_contour_fill((aes(z=hgt,fill=stat(level)))+
geom_contour(aes(z=hgt),color="black",size=0.2)+
scale_fill_distiller(palette = "YlOrBr",direction = 1,super=ScaleDiscretised)+
mi_mapa+
coord_quickmap(xlim = range(dataframe$lon),ylim=range(dataframe$lat),expand = FALSE)+
facet_wrap(~nombre_nivel,scales="free", ncol =2) +
labs(x="Longitud",y="Latitud",fill="altura",title = "campos")
my dataframe has a shape like this. Where the facets are determined by the level variable. In this case the dataframe has another variable which is temp instead of hgt, but it's just another name.
enter image description here
Thanks
I think I've faced the alike problem building the two parts of the single map with two different scales. I found the package grid useful.
library(grid)
grid.newpage()
print(myggplot, vp = specifiedviewport)
In my case I built the first p <- ggplot() than adjusted p2 <- p + ...
with printing the two ggplots (p and p2) in two viewports. You can newly construct p2 with individual scale and print it in the grid. You can find useful information
here.
I try to generate a plot on which every point stands for an event. Color, Size and faced_grid are used to give additional information available in a visual way. The graph is working in ggplot2 but it is often important to know the exact numbers so an interactive version is needed which enables to hover over the point and get the info. I tried to convert the plot into an interactive version with the function ggplotly from the plotly-package. The problem then is, that the legend not only display the different states of the used attributes, it contains every existent combination. In addition, it did not display info from geom_rect.
I found related/similar questions but they used the function plot_ly and not ggploty or did not provide an answer.
Following, the same problem illustrated with the mtcars dataset:
library(plotly)
g = ggplot(mtcars,aes(x=mpg,y=disp,color = as.factor(cyl),size =as.factor(gear))) +
geom_point() +
geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)),color = "black",size=4) +
geom_rect(data=data.frame(name="zone",Start=20,End = 30,ymin = -Inf,ymax = Inf),aes(xmin=Start, xmax=End, ymin=ymin, ymax=ymax,fill=name),inherit.aes = FALSE,alpha=0.3)+
facet_grid(vs~am)
g
This is the result and how it should look like: ggplot Graph
Now using ggplotly
ggplotly(g)
This is the result: ggploty Graph
(1) The legend is now a combination of the different attributes used for Color and Size
(2) geom_rect is in the legend but didn’t get displayed in the graph
Does anyone knows how to get the same graph in ggplotly like in ggplot2? I am grateful for every hint. Thanks
Dave
I do not know how to fix the combination of legends when you use ggplotly. But, I can fix the second problem, if you do not use the Inf and -Inf, the geom_rect will work:
ggplotly(ggplot(mtcars,aes(x=mpg,y=disp, = as.factor(cyl),size =as.factor(gear))) +
geom_rect(aes( xmin=20,
xmax=30,
ymin=0,
ymax=max(mtcars$disp),
fill="Name"),
inherit.aes = FALSE, alpha=0.3) +
geom_point() +
geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)), = "black",size=4) +
facet_grid(vs~am))
However, the legends are bad.
I would suggest using subplot to create the same thing in Plotly, and I think this link Ben mentioned will help you create each subplot. One thing to mention is that I had trouble Illustrating different size in legend in plotly, while the size of the marker will be different, there will not be a legend for the size scale. Maybe a scale will be a better option.
Using the likert package of R (more information: http://jason.bryer.org/likert/) to create these graphs which uses ggplot2.
Here is some example code to generate a graph using likert in R.
require(likert)
data(pisaitems)
items28 <- pisaitems[, substr(names(pisaitems), 1, 5) == "ST24Q"]
l28 <- likert(items28)
plot(l28)
Here is an example of what I want the output to show instead:
Firstly, I was wanting to add percentages into each box in the likert scale to represent the amount, instead of just in the middle like it currently offers. I have looked through the source code on github and can't understand how he does this.
Secondly, I was wondering it it were possible to have a subheading over each side of the middle divide to say "percentage of very low/low" and "percentage of high/very high" over on top of the graph which stays in relation to the rest of the plot.
Thanks
To add the percentages you just need to set the argument plot.percents to TRUE. I don't know a good solution for the subheading, but you can play around with the ggtitle and theme functions of the ggplot2 package. Here is one idea:
plot(l28, plot.percents = TRUE) +
ggtitle("Percentage of Strongly disagree/Disagree Percentage of Strongly agree/Agree") +
theme(plot.title = element_text(hjust = 0.45, size = 10))
I am new to using qplot and ggplot, and basically want to make a figure that is just the combination of a bar plot and a line plot. I can do one or the other, but don't know how to do both at once!
Here is my data:
bulk = data.frame(x_pos=c(1,2,3,4,5,6,7,8),
y_line=c(3,7,6,8,14,16,18,12),
y_bar=c(0,0,10,0,0,0,10,0))
For a line graph, I just do qplot(x_pos, y_line, data=bulk, geom="line")
For a bar plot, I just do qplot(x_pos, y_bar, data=bulk)
But! How can I combine these at once into a single figure?? My real intention is to use several (maybe 6-10) different graphics techniques like this to generate complex figures, but it all starts with knowing how to do two at once. Thanks for any help!
Don't use qplot for this.
library(ggplot2)
ggplot(bulk, aes(x=x_pos)) +
geom_bar(aes(y=y_bar), stat="identity") +
geom_line(aes(y=y_line), color="red", size=2)
I've been trying to create a 3D bar plot based on categorical data, but have not found a way.
It is simple to explain. Consider the following example data (the real example is more complex, but it reduces to this), showing the relative risk of incurring something broken down by income and age, both categorical data.
I want to display this in a 3D bar plot (similar in idea to http://demos.devexpress.com/aspxperiencedemos/NavBar/Images/Charts/ManhattanBar.jpg). I looked at the scatterplot3d package, but it's only for scatter plots and doesn't handle categorical data well. I was able to make a 3d chart, but it shows dots instead of 3d bars. There is no chart type for what I need. I've also tried the rgl package, but no luck either. I've been googling for more than an hour now and haven't found a solution. I have a copy of the ggplot2 - Elegant Graphics for Data Analysis book as well, but ggplot2 doesn't have this kind of chart.
Is there another freeware app I could use? OpenOffice 3.2 doesn't have this chart either.
Thank you for any hints.
Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11
I'm not sure how to make a 3d chart in R, but there are other, better ways to represent this data than with a 3d bar chart. 3d charts make interpretation difficult, because the heights of the bars and then skewed by the 3d perspective. In that example chart, it's hard to tell if Wisconsin in 2004 is really higher than Wisconsin 2001, or if that's an effect of the perspective. And if it is higher, how much so?
Since both Age and Income have meaningful orders, it wouldn't be awful to make a line graph. ggplot2 code:
ggplot(data, aes(Age, Risk, color = Income))+
geom_line(aes(group = Income))
Or, you could make a heatmap.
ggplot(data, aes(Age, Income, fill = Risk)) +
geom_tile()
Like the others suggested there are better ways to present this, but this should get you started if you want something similar to what you had.
df <- read.csv(textConnection("Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11
"))
df$Age <- ordered(df$Age, levels=c('young', 'adult', 'old'))
df$Income <- ordered(df$Income, levels=c('low', 'medium', 'high'))
library(rgl)
plot3d(Risk ~ Age|Income, type='h', lwd=10, col=rainbow(3))
This will just produce flat rectangles. For an example to create nice looking bars, see demo(hist3d).
You can find a starting point here but you need to add in more lines and some rectangles to get a plot like you posted.