I have following problem that I have to place the bars at dedicated x-values.
I have a series, say of a frequency (say from 0 to 100) where i want to show the values for dedicated frequencies.
For example:
width : 1 1 3 2 4
height: 6 2 1 5 4
x-frequency for example: 1,2,3 6,12 is where to plot the bars
when i do the barplot:
barplot(width, height)
axis(1,,seq(0,15,1))
i get the following:
But that is not what i want, i want the bars at locations according to my values 1,2,3 6,12
I´ve found a solution: since barplot does the positioning, i would have to do it another way, i can place a bar with the "rect" command:
rect(xleft=3, ybottom=0, xright=5, ytop=6)
This is exactly what i wanted.
Related
barplot(11:20)
The above command plot bars at positions roughly from 1 to 10. But the starts and ends are not exact at 1 and 10.
lines(0:10, 0:10)
How to control exactly where the bars are located?
I am trying to include the count labels on stacked bar plots which represent percentages. I want to show x-amount of individuals make up the graphed percentages. However, when I include the count labels my y-axis gets blown out of proportion because it changes to match the count data, not the percentages. Also, the bars are removed from the graph too? I have reviewed other posts similar to this, such as: "How to add percentage or count labels above percentage bar plot?". I cannot find the error in my r command.
My command used is as follows:
sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+
geom_bar(position="fill",stat="identity")+
geom_text(aes(label=nDet),position=position_stack(vjust=0.5))+
theme(axis.text.x=element_text(angle=90,hjust=1))+
scale_y_continous(labels=scales::percent_format())
Example of data being graphed:
speciesSci recvDeployName nDet
1 Arenaria interpres Bucktoe Preserve 96
2 Arenaria interpres CHDE 132
3 Arenaria interpres Fortescue 22133
4 Arenaria interpres Mispillion 2031
5 Arenaria interpres Norbury 3709
6 Arenaria interpres Penn - DRL 49
What my graph looks like when I use the command example provided above:
11/17/19 Update: The r commands below seems promising:
sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+
geom_text(aes(label=nDet),position="fill", stat="identity")+
theme(axis.text.x=element_text(angle=90,hjust=1))+
scale_y_continuous(labels=scales::percent_format())
I just need to get the colored bars back onto the graph, representing the percentages.
Any help would be greatly appreciated. Thank you.
This gave me the result I wanted:
sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=factor(speciesSci))+geom_bar(positon="fill", stat="identity")+geom_text(aes(label=nDet),position = position_fill(vjust=0.5))+theme(axis.text.x=element_text(angle=90,hjust=1))+scale_y_continuous(labels=scales::percent_format()))
Correct Graph
I've got a matrix that I've plotted using the bar plot function to create a stacked bar plot of changes in percent of something (alphabets) over time. The matrix looks like:
Year V W X Y Z
1 7.5397 20.6349 11.1111 0.3968 60.3175
2 8.3333 21.4286 11.9048 0.3968 57.9365
3 23.8095 9.5238 9.5238 0.3968 56.7460
4 23.4127 10.7143 10.3175 1.1905 54.3651
5 23.4127 11.1111 10.7143 1.9841 52.7778
6 30.0000 2.4000 19.2000 2.4000 46.0000
All these should be the same height, because they add to 100, but in my plot, all the heights are just a bit different at the top. My bar plot code looks like this:
>barplot(t(as.matrix(mydata[,2:6])),names.arg=unique(mydata$Year),axis.lty=1, ylim=c(0,100),
xlab="Year", ylab="Percent of Categories", etc.
I've tried adjusting the ylim, changing xpd to =FALSE, and making some axis argument adjustments, and I'm not sure what else to try before scratching the whole thing and retrying with a different plotting function. Any help is appreciated.
I got some strange results trying to plot the histograms of a pretty standard random variable with ggplot.
RB = rbinom(10000000,100,.3)
qplot(RB)#histogram of the distrib with ggplot2. Assumes 30 buckets by default
dev.new()
hist(RB,breaks=30)#same with regular histogram
dev.new()
qplot(RB,binwidth=1)#if we force binwidth to 1 it seems to work
dev.new()
hist(RB,breaks=range(RB)[2]-range(RB)[1])
The result of the first call to qplot is quite odd. With the numbers of draws we expected the graph to show a smooth distribution.
I use ggplot2 version 1.0.0 and R 3.0.2
By default, ggplot uses range/30 as binwidth, as prompted. In your case, it is approximately 48/30 (depends on the seed), which is more than 1 and is around 1.5.
Now, your data is not continuous, you only get integers, so for any two adjacent histogram bins you'll get irregularities, caused by the fact that the first bin will only contain one possible integer, and the next will contain two, and so on. As a result, you'll see the count approximately doubled for every second bin.
Say, your data looks like
1 2 3 4 5 6
5 5 5 5 5 5
and if you start counting from 0.5, you'll get these bins:
(0.5, 2] (2, 3.5] (3.5 5] (5, 6.5]
10 5 10 5
which is exactly those spikes you see on the first of your plots.
As you have already found out, this won't be a problem if binwidth is strictly 1.
Edit:
as pointed out by #James, use the following to reproduce the picture given by ggplot with base graph:
hist(RB, breaks=seq(min(RB), max(RB), length.out=30))
It may look a bit different, but the spikes are there.
I have two vectors: x and y. I'm plotting them with plot(x,y, type="l"). However I want to show the detailed values on the x-axis between the plotted values on the x-axis. Now I have 0 ... 20 ... 40 ... I want to show 0 1 2 3 4 5 6 ... 20 and I want them to be in smaller size than the main values. How can I do that?
Here is the answer to your question
grid = 1:100
x = rnorm(100)
plot(x,type='l')
axis(1,grid[c(-20,-40,-60,-80,-100)],grid[c(-20,-40,-60,-80,-100)],cex.axis=.5,line=-1,tick=FALSE)
Use axis to customize it. For example:
plot(seq(1,100,10), rnorm(10),type='l',cex.axis=2,
lwd.ticks=5)
axis(1, 1:100[-c(20,40,60,80,100)],
1:100[-c(20,40,60,80,100)],tick=TRUE,
cex.axis=0.8)