R: in barplot midpoints are not centered w.r.t. bars
If you save the barplot()
result as an object you get the midpoints for the bars.
bp <- barplot(y)
bp
[,1]
[1,] 0.7
[2,] 1.9
[3,] 3.1
[4,] 4.3
[5,] 5.5
If you use them now in other plotting functions those midpoints should be as x
values. In call plot(bp)
they are used as y
values and x
values are sequence numbers 1,2,3,4,5
- so they do not correspond to midpoints.
Providing also y
values, points are plotted as expected.
bp <- barplot(y)
points(bp,c(10,20,30,40,50))
Points of dotplot are not centered on the corresponding barplot bars
As mentioned by @s_t, you have to write:
library(ggplot2)
ggplot(d, aes(x=a, y=d, fill=as.factor(c))) +
geom_bar(stat="identity", position="dodge")+
geom_point(aes(y = b / 10000),
position = position_dodge(.9))
Data
d = data.frame(a = sort(rep(LETTERS[1:5],2)),
b = c(55,123,222,234,233,187,564,325,112,105),
c = rep(c(2,1),5),
d = c(0.07,0.05,0.04,0.03,0.03,0.04,0.06,0.01,0.02,0.01))
How to center bars in R barplot() around ticks
Part1 <- c(2,4,9,18,20)
Part2 <- c(2,5,1,4,0)
counts <- rbind(Part1, Part2)
colnames(counts) <- c(1,2,3,4,5)
x<-barplot(counts,
axes = FALSE,
space = 0,
col = c("darkgreen", "red"),
xlim = c(0, 5*1.50),
ylim = c(0,60)
)
#create positions for tick marks, one more than number of bars
ticks <- seq_len(length(counts) + 1)
axis(side = 2, pos = 0)
#adding x-axis with offset positions, with ticks, but without labels
axis(side = 1, at = ticks - 0.5, labels = FALSE)
Labels do not fit to my bin center in histogram
The x
axis values are not necessarily interger values. The barplot
function returns the x
values used during plotting. Try
bp<-barplot(data.norm, beside=TRUE, col=c("grey10","grey20","grey30","grey40","grey50","grey60"))
text(bp, par("usr")[3], labels=my.names, srt=45, pos=2, xpd=TRUE,offset=0.01)
instead.
Labelling axis in a plot
You mean something like this?
Which is based on this post.
There are probably more sophisticated ways to do this.
# Barplot
bp<-barplot(as.matrix(condition),
col=c("darkblue","red"),
xlab="month",
ylab="subject count",
main="Monthly condition",
ylim=c(0, 140))
# x-axis labels
axis(1, at = bp,
labels=c("month 1", "month 2", "month 3", "month 4", "month 5"),
cex.axis=1.2)
# Add legend
legend(5.25,140.1,
c("good","poor"),
fill=c("darkblue","red"),
title="condition")
Which will give:
You probably want to do something on the position of your legend, and I don't think that specifying the xlab
is necessary if you're going to label each bar individually.
I won't comment on the choice of colours :)
Related Topics
How to Annotate Ggplot2 Qplot Outside of Legend and Plotarea? (Similar to Mtext())
Place Text Values to Right of Sankey Diagram
Create a Variable That Identifies the Original Data.Frame After Rbind Command in R
R Bnlearn Eval Inside Function
How to Sort a Vector of Alphanumeric Values Using Lexical Ordering in R
Changing Names in a List of Dataframes
R: Calculate the Number of Occurrences of a Specific Event in a Specified Time Future
Unexpected Date When Converting Posixct Date-Time to Date - Timezone Issue
Creating a Prng Engine for <Random> in C++11 That Matches Prng Results in R
Changing the Order of Dodged Bars in Ggplot2 Barplot
R: in Barplot Midpoints Are Not Centered W.R.T. Bars
Using Lubridate and Ggplot2 Effectively for Date Axis
R Geom_Tile Ggplot2 What Kind of Stat Is Applied
Out of Order Text Labels on Stack Bar Plot (Ggplot)
How to Create a Single Dummy Variable with Conditions in Multiple Columns