Error in pie3D (plotrix) in r - r

Lets make some data:
dat <- data.frame(art=c("Ål", "Gedde", "Brosme"), sum=c(708,3797,1385))
And when i plot this in a 3D plot, like this:
library(plotrix)
pie3D(dat$sum,labels=dat$art,explode=0.1, main="Arter")
This happens:
How can I avoid that red are below green?

Sometimes it helps to read the help:
Due to the somewhat primitive method used to draw sectors, a sector that extends beyond both pi/2 and 3*pi/2 radians in either direction may not display properly. Setting start to pi/2 will often fix this, but the user may have to adjust start and the order of sectors in extreme cases
pie3D(dat$sum,labels=dat$art,explode=0.03, start=pi/2, main="Arter")
(also, the explode=0.03 looks nicer imho)

Related

cluster: :clusplot axis in wrong direction

I'm trying to plot the cluster obtained from fuzzy c-means clustering.
The plot should look like this.
code for the plot
plot(data$Longitude, data$Latitude, main="Fuzzy C-Means",col=data$Revised, pch=16, cex=.6,
xlab="Longitude",ylab="Latitude")
library(maps)
map("state", add=T)
However, when I tried to use clusplot the plot is displaying in opposite direction(both top and bottom and left and right) as below.
I wanna know if there's a way to reverse the plot to show in the order as the above picture.
Also, for the very dense area, it's hard to find the ellipse label. I wanna know if there's a way to show the label inside the ellipse instead of outside.
code for 2nd pic
library(cluster)
clusplot(cbind(Geocode$Longitude, Geocode$Latitude), cluster, color=TRUE,shade=TRUE,
labels=4, lines=0,col.p=cluster,
xlab="Longitude",ylab="Latitude",cex=1)
clusplot is a function that performs a lot of magic for you. In particular it projects the data set - which happens in a way you don't like, unfortunately. (Also note the scales - it centered and scaled the data, too)
clusplot.default: Creates a bivariate plot visualizing a partition (clustering) of the data. All observation are represented by points in the plot, using principal components or multidimensional scaling.
As far as I can tell, clusplot doesn't have map support, but you will want such a map I guess...
While maybe you can use the s.x.2d parameter to specify the exact projection (and this way disable automatic scaling), it probably is still difficult to add the map. Maybe look at the source of clusplot instead, and take only the parts you want?

Plotting straight surface with lattice::wireframe()

Assume I want to plot the following dataframe:
df <- data.frame(expand.grid(1:10,1:10),rep(10,100))
colnames(df) <- c("x","y","z")
with the lattice wireframe() function:
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE)
How do I get it to plot the given coordinates? I would assume it has something to do with having to scale/adjust the z-axis as the automatic scaling within wireframe is probably confused by all z-coordinates being equal.
This is from the help page scales section: "The most common use for this argument is to set arrows=FALSE, which causes tick marks and labels to be used instead of arrows being drawn (the default)." So just add that as a list value to 'scales':
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE,
scales=list(arrows=FALSE), zlim=c(0,10.1))
The failure of wireframe to display anything when the plotted plane is at one of the extremes seems to be at least "unexpected behavior" if not a bug. I suspect you would not see this in real data. Your use of drape doesn't make much sense since the entire data-plane plane gets displayed at the white midpoint. (Again this is probably not a problem if you have something other than this pathological example.)
Just add a zlim argument.
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE, zlim=c(0,20))

ggplot2 ecdf behaviour seems odd

Consider the following series and cumulative plot:
x=c(0,0,0,0.5,10,1500)
qplot(x,geom='step',stat='ecdf')
This produces a graph that starts left of zero. In other words, it looks as if x has negative values. At the right it continues to the right after reaching 1500 and 100%. So, it looks as if there are x points larger than 1500.
I get what I expect when doing the whole thing manually:
xs=sort(x)
qplot(xs,1:length(xs)/length(xs),x,geom='step')
But this seems to defy the whole purpose of the stat='ecdf' shortcut.
What am I missing?
By default stat_ecdf will pad the endpoints by max(0.08 * diff(rx), median(diff(xvals))). In my answer to In R ggplot2, include stat_ecdf() endpoints (0,0) and (1,1) I give a way of working around this, but it might be a little drastic, depending on your use case.

Drawing circles in R

I'm using plotrix package to draw circles.
And I don't get what is wrong with my code... :-(
I have three points. The first point (1,1) should be the center of the circle. The following two points (1,4) and (4,1) have the same distance/radius to the center.
So the circle in the plot should go through these points, right?
And I don't know why the circle looks wrong. Is there an explanation?
p1 <- c(1,1)
p2 <- c(4,1)
p3 <- c(1,4)
r <- sqrt(sum((p1-p2)^2))
plot(x=c(p1[1], p2[1], p3[1]),
y=c(p1[2], p2[2], p3[2]),
ylim=c(-5,5), xlim=c(-5,5))
draw.circle(x=p1[1], y=p1[2], radius=(r))
abline(v=-5:5, col="#0000FF66")
abline(h=-5:5, col="#0000FF66")
Take a look at the produced output here
As #Baptiste says above, you can use plot(...,asp=1). This will only work if your x and y ranges happen to be the same, though (because it sets the physical aspect ratio of your plot to 1). Otherwise, you probably want to use the eqscplot function from the MASS package. A similar issue arises whenever you try to do careful plots of geometric objects, e.g. Drawing non-intersecting circles
This plot is produced by substituting MASS::eqscplot for plot in your code above:
Note that depending on the details of what R thinks about your monitor configuration etc., the circle may look a bit squashed (even though it goes through the points) when you plot in R's graphics window -- it did for me -- but should look OK in the graphical output.

R plot axes don't meet and data extends beyond them

I have a VERY basic plot in R, and I'd like to solve two issues. Here is the code which produces the plot:
plot(o,n,bty="n",pch=21,cex=1.5,bg="gray",xlab="y",ylab="x",lwd=2)
And, here's the plot
There are two unwanted behaviors of this plot that I'm trying to fix. And I don't know how to do either one (nor do I understand why R doesn't do these things already...)
The X and Y axes do not meet. There is a gap near the origin in this plot. I want to remove that. The axes should touch, just like any other graph.
The data extends past the axis is both the X and Y direction. This clearly is unwanted. How can I fix this without having to manually make my own axis. Seems like there should be something more intuitive here.
bty="l".
You may also want to use something like:
xlim=c(0.02, 0.24), ylim=c(0.02, 0.24)
if you don't like the default limits of your two axes.
In general, check out ?par for guidance on both of these and many other options.
Try leaving out bty="n" or replacing it by bty="L" if you really do not want a box with edges above or on the right

Resources