This question already has an answer here:
Ordering of points in R lines plot
(1 answer)
Closed 3 years ago.
I used plot(x, y, type="p") to draw a scatter plot, and it seems right (Figure 1). However, when using plot(x, y, type="l") to draw a line, there are some mussy lines (Figure 2). Why didn't it a "single" line?
Looks like your x vector needs to be sorted, when using line plots, the order in which your points are submitted is very important as the lines are drawn connecting one point to the next one.
y <- y[order(x)]
x <- x[order(x)]
# now you can make your plot
plot(x, y, type="l")
Related
This question already has answers here:
Plotting half circles in R
(3 answers)
Closed 2 years ago.
I'd like to add points that are multicoloured dots, with one half in blue for ex. and one half in red. If possible, I'd like to be able to do it with more than 2 colors, for example 4 quarters, each with a color.
Is that at all possible?
I know I can superimpose several dots of different sizes, each with a color, resulting in a concentric multicolored dot. But this is not what I am after.
For context, I am using these points for cities in a map.
Here is a small example, with each city having a unicolored dot. But I would like each city to have a dot that has two halves, each one with a color.
require(ggmap);
citiesnames=c("Madrid","Toledo","Valencia","Granada")
cities=str_c(citiesnames,"Spain",sep=", ");
geo=geocode(cities);
lon=geo$lon;
lat=geo$lat;
coord=mapproject(lon, lat,proj="mercator");
map("world","Spain",fill=T,col="ivory",proj="mercator");
points(coord$x[1], coord$y[1], pch=16, cex=1.2, col="black")
points(coord$x[2],coord$y[2], pch=16, cex=1.2, col="red")
points(coord$x[3], coord$y[3], pch=16, cex=1.2, col="blue")
points(coord$x[4], coord$y[4], pch=16, cex=1.2, col="green4")
Any help greatly appreciated.
As I mentioned in the comments you can refer to this post for doing what you want. For documentation purposes I add an answer which resolve the issue:
You can add two half circles (pie charts) to your graph using these two functions:
upper.half.circle <- function(x,y,r,nsteps=100,...){
rs <- seq(0,pi,len=nsteps)
xc <- x+r*cos(rs)
yc <- y+r*sin(rs)
polygon(xc,yc,...)
}
lower.half.circle <- function(x,y,r,nsteps=100,...){
rs <- seq(0,pi,len=nsteps)
xc <- x-r*cos(rs)
yc <- y-r*sin(rs)
polygon(xc,yc,...)
}
Later when you are piloting you should set asp = 1 to get the half-circles.
plot(1, type="n",axes=F,xlab="", ylab="",xlim=c(0,200),ylim=c(0,200), asp = 1)
upper.half.circle(15,170,10,nsteps=1000,col='red')
lower.half.circle(15,170,10,nsteps=1000,col='blue')
Here's the result of this code:
You can later edit the fictions for getting quarters or whatever you need.
This question already has answers here:
3D equivalent of the curve function in R?
(4 answers)
Closed 8 years ago.
If x and y can vary from 0 to 10, how can I plot a maths function z = x*(y^2) as an image plot or heatmap ? The x and y should come to their respective axes and z value should be shown as colour on the plot. I could find method to plot only discrete values, not a continuous function. Thanks for your help.
For example
library(emdbook)
curve3d(x*y^2,xlim=c(0,10),ylim=c(0,10),sys3d="image")
curve3d is just a wrapper (the sys3d argument gives you a range of plotting possibilities) -- more generally, you can create a function and use outer() (if it's vectorized) to create a matrix, e.g.
xvec <- yvec <- seq(0,10,length.out=41)
z <- outer(xvec,yvec,function(x,y) x*y^2)
image(xvec,yvec,z)
This question already has answers here:
How to specify the actual x axis values to plot as x axis ticks in R
(4 answers)
Closed 9 years ago.
Let's say I have some points:
a=c(1.234, 23.332, 3.433, 34.53)
b=c(112, 234, 221, 23)
I would like to plot them by having "a" on the x axis and "b" on the y axis.
plot(a,b)
This will give the resulting plot, but what I would like to do is rather than display some interval on the x axis I would like to display the exact numbers that I have, i.e. the values of a.
Is this possible?
Jup.
plot(a,b, xaxt="n")
axis(side=1, at=a)
or maybe
axis(side=1, at=round(a,2))
for aesthetical reasons.
This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.
This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 5 years ago.
Hi I'm making histogram using R, but the number of Y axis is so large that I need to turn it into logarithmic.See below my script:
hplot<-read.table("libl")
hplot
pdf("first_end")
hist(hplot$V1, breaks=24, xlim=c(0,250000000), ylim=c(0,2000000),main="first end mapping", xlab="Coordinates")
dev.off()
So how should I change my script?
thx
You can save the histogram data to tweak it before plotting:
set.seed(12345)
x = rnorm(1000)
hist.data = hist(x, plot=F)
hist.data$counts = log10(hist.data$counts)
dev.new(width=4, height=4)
hist(x)
dev.new(width=4, height=4)
plot(hist.data, ylab='log10(Frequency)')
Another option would be to use plot(density(hplot$V1), log="y").
It's not a histogram, but it shows just about the same information, and it avoids the illogical part where a bin with zero counts is not well-defined in log-space.
Of course, this is only relevant when your data is continuous and not when it's really categorical or ordinal.
A histogram with the y-axis on the log scale will be a rather odd histogram. Technically it will still fit the definition, but it could look rather misleading: the peaks will be flattened relative to the rest of the distribution.
Instead of using a log transformation, have you considered:
Dividing the counts by 1 million:
h <- hist(hplot$V1, plot=FALSE)
h$counts <- h$counts/1e6
plot(h)
Plotting the histogram as a density estimate:
hist(hplot$V1, freq=FALSE)
You can log your y-values for the plot and add a custom log y-axis afterwards.
Here is an example for a table object of random normal distribution numbers:
# data
count = table(round(rnorm(10000)*2))
# plot
plot(log(count) ,type="h", yaxt="n", xlab="position", ylab="log(count)")
# axis labels
yAxis = c(0,1,10,100,1000)
# draw axis labels
axis(2, at=log(yAxis),labels=yAxis, las=2)