Reputation: 37
I have data that looks like:
0.2 0.8 val1
0.3 0.7 val1
0.9 0.1 val1
0.22 0.78 val2
0.30 0.70 val3
0.00 1.00 val3
What I would like is a barplot where each row is plotted as its own bar but in the names for the x-axis, I would don't want "val1" 3 times, I just want it once and centralized (with respect to the val1 bars). I googled a lot but do not know what to google honestly.
Upvotes: 0
Views: 276
Reputation: 67778
Another ggplot
alternative:
df <- read.table(text = "
0.2 0.8 val1
0.3 0.7 val1
0.9 0.1 val1
0.22 0.78 val2
0.30 0.70 val3
0.00 1.00 val3")
names(df) <- c("y1", "y2", "x1")
df$x2 <- 1:nrow(df)
df2 <- melt(df, id.vars = c("x1", "x2"))
xlabs <- ddply(.data = df, .variables = .(x1), summarize, xval = median(x2))
labs$x1 <- labs$x1
ggplot(data = df2, aes(x = x2, y = value, fill = variable)) +
geom_bar(stat = "identity") +
scale_x_continuous(breaks = labs$xval, labels = labs$x1) +
xlab("")
Upvotes: 0
Reputation: 93908
Something like this maybe?
dat <- read.table(text="0.2 0.8 val1
0.3 0.7 val1
0.9 0.1 val1
0.22 0.78 val2
0.30 0.70 val3
0.00 1.00 val3",header=FALSE)
bp <- barplot(t(as.matrix(dat[1:2])))
axis(1,at=tapply(bp,dat[,3],mean),labels=unique(dat[,3]))
I don't know how you're going to differentiate what label belongs to what bar though - the result is really quite confusing with "6 distinct bars but 3 labels on the x axis" as requested.
To avoid the grouping confusion you could always do something like:
bp <- barplot(t(as.matrix(dat[1:2])))
mapply(
function(a,b,c) axis(1,at=c(a,b,c),labels=FALSE),
tapply(bp,dat[,3],tail,1),
tapply(bp,dat[,3],head,1),
tapply(bp,dat[,3],mean)
)
axis(1,at=tapply(bp,dat[,3],mean),labels=unique(dat[,3]),lwd=NA)
Upvotes: 0
Reputation: 15458
> mydata
col1 col2 variable
1 0.20 0.80 val1
2 0.30 0.70 val1
3 0.90 0.10 val1
4 0.22 0.78 val2
5 0.30 0.70 val3
6 0.00 1.00 val3
library(ggplot2)
mm <- ddply(mydata, .(variable), summarise, mecol1 = mean(col1),mecol2=mean(col2))
ggplot(mm, aes(x = factor(variable), y = mecol1)) + geom_bar(stat = "identity")
ggplot(mm, aes(x = factor(variable), y = mecol2)) + geom_bar(stat = "identity")
Upvotes: 0
Reputation: 263411
barplot(t(as.matrix(dat[1:2])),
names.arg=c("",as.character(dat$V3[2]),"", as.character(dat$V3[4:6])))
Upvotes: 1