I'd like to draw a pie chart where the order of values and labels must be preserved. How could I produce it?
It seems that by default R can control for alphabetical and clockwise order.
I addition, I'd like to add more information to the chart by adding arrows connecting one point to the other of the chart, like from ABC to ZXR, is it any possible?
# Simple Pie Chart
slices <- c(12,11,11,8,6,2,5,3)
lbls <- c("ABC", "DEF", "GPH", "ZXR", "O", "MN", "TS", "ZY")
pie(slices, labels = lbls, main="ranking disk")
Thanks,
Eve
The following will create a pie chart with the labels in the order you posit them, starting at 12 o'clock and going around clockwise:
pie(slices, labels = lbls, clockwise=TRUE, init.angle=90, main="ranking disk")
You can adapt the clockwise and init.angle parameters.
Unfortunately, pie does not return the coordinates of labels or slices, but you can add arrows by hand if you adapt the coordinates as you need them:
arrows(.6,.6,-.6,-.4)
However, you do know that pie charts are horrible ways of presenting information, right? The following part of the help page ?pie is well worth quoting in full:
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.
Related
I was trying to draw a boxplot with all the points on it. I need to use multiple colors to show multiple dimensions.
Three boxes with three colors (say, January, February, March in X axis)
Some values in Y
All the points in different (Say, different temperatures - 10 steps in 10) colors << This is the challenge I am confused with.
In the legend only color-point should appear << Also this.
I could do it in ggplot. Tried to convert ggplot to plotly like this,
qq <- ggplotly(gg). It does not work, legends just break.
Could you please drop some light on it? Thank you.
I'm having a problem with the color bar (or color ramp palette) in spplot (adehabitatHR package). I'm want to plot two graphs in the same window using spplot and I also want that the color bar has the same scale for both graphs (just one bar for both graphs). If that is not possible - or too complicated, since I'm new in R language - I wanna set an equal number of tick marks in the bar for both graphs. I had setted this already, but they are in different positions in the color bar - it would make more sense if i have at least one tick mark at lowest value of the bar, one in the middle, and other in the highest valeu. But when I use the colorkey=list(tick.number=3) argument it doesn't allow me to choose where I want the tick marks. How can I do that?
Here is part of my code.
####loading packages....
library("adehabitatHR")
library(latticeExtra)
###... and data
area.total.mari<- read.table("mari.total.txt")
area.total.mari2<- SpatialPoints(area.total.mari)
area.total.mari.mpc=mcp(area.total.mari2)
dry.mari<- read.table("pontos meses seca.txt")
dry.mari1<- SpatialPoints(dry.mari)
dry.mari1.mpc=mcp(dry.mari1)
###color used in the graphs
mycolor2=gray(16:0/16)
##generating kernels and spplot's
ks<-kernelUD(seca.mari1, grid=300, extent=0.2)
ps<-spplot(ks, col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T))+
layer(sp.polygons(area.total.mari.mpc))
kc<-kernelUD(chuva.mari1, grid=300, extent=0.35)
pc<-spplot(kc,col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T)))+
layer(sp.polygons(area.total.mari.mpc))
###printing graphs
######## Tick marks are messed up :/
print(ps, position=c(0,0,.5,1),more=T)
print(pc, position=c(0.5,0,1,1))
Here is the graph generated with this code:
https://www.flickr.com/photos/129526227#N02/16316924041/
Note the problem with the tick marks: the lowest value ("0") is ok, but I can't place the another value in the highest!
This is an example with the "meuse" data set:
library(sp)
library(lattice)
library(adehabitatHR)
library(latticeExtra)
data(meuse)
coordenadas<-data.frame(meuse$x,meuse$y)
attach(coordenadas)
coord.sp.pt<- SpatialPoints(coordenadas)
coord.sp.pt.mpc=mcp(coord.sp.pt)
mycolor=gray(0:12/12)
coord.sp.ud<-kernelUD(coord.sp.pt, grid=300, extent=0.1)
coord.printing<-spplot(coord.sp.ud, col.regions=colorRampPalette(c("white","gray","black"))
,colorkey=list(tick.number=2),scales=list(draw=T))+
layer(sp.polygons(coord.sp.pt.mpc))
print(coord.printing)
Thanks in advance folkes
Say I have some data on some experiment I conducted on Earth and on Wayne's World. There are control and treatment means:
means1<-c(1,2)
means2<-c(1.5,2.5)
data<-cbind(means1,means2)
rownames(data)=c('ctrl','treatment')
colnames(data)=c('Earth','Waynes World')
I would like to plot this data, so I do.
barplot(data,beside=T)
This generates paired control and treatment bars, separated by planet. Each pair of bars has an x axis label specifying what planet they are from. What I would like is a second set of x-axis labels underneath each bar that specifies ctrl or treatment. Bonus if you tilt this second set of labels, they don't overlap the first labels, and everything looks pretty.
I think something like this describes what you're after
bp<-barplot(data,beside=T, xaxt="n")
mtext(text=rownames(data)[row(bp)], at=bp, line=1, side=1)
mtext(text=colnames(data), at=colMeans(bp), line=2.2, side=1)
I am trying to add a legend to the outside of a plot in R.
What I am using is:
png(height=400,width=842,"./rainfall.png")
par(family="serif",mar=c(4,6,4,1),oma=c(1,1,1,6),mfrow=c(1,2))
I create my plot, then:
par(xpd=TRUE)
legend(x="topright",inset=c(-0.2,0),c("4 year moving average","Simple linear trend"),lty=1,col=c("black","red"),cex=1.2)
legend("topleft",c("Annual total"),pch="*",col="blue",cex=1.2)
dev.off()
When i do this though the legend is cut off on the right, as shown in the image below. How can I get the legend to be visible outside the plot?
http://imgur.com/rpgVyrA
Just to let you know, I have been trying the suggestions in this thread, ut they are not working for me: Plot a legend outside of the plotting area in base graphics?
Any help would be appreciated,
Ciara
?par, look for xpd:
A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip.
Use xpd=NA so the legend is not cut off by the plot or figure region.
legend(x="topright",inset=c(-0.2,0),c("4 year moving average",
"Simple linear trend"),lty=1,col=c("black","red"),cex=1.2, xpd=NA)
Results:
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.