I want to develop the following type of graph:
Where the postion determines the position of bar (not single direction rather both direction, although direction do not have special sense but aesthetic to look like a map) and height determines height of bar at each position. The following is corresponding dataset.
position <- c(0, 1, 3, 4, 5, 7, 8, 9, 0, 1, 2, 4.5, 7, 8, 9)
group <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2)
barheight <- c(0.5, 0.4, 0.4, 0.4, 0.6, 0.3, 0.4, 1, 0.75, 0.75,
0.75, 1, 0.8, 0.2, 0.6)
mydf <- data.frame (position, group, barheight)
mydf
position group barheight
1 0.0 1 0.50
2 1.0 1 0.40
3 3.0 1 0.40
4 4.0 1 0.40
5 5.0 1 0.60
6 7.0 1 0.30
7 8.0 1 0.40
8 9.0 1 1.00
9 0.0 2 0.75
10 1.0 2 0.75
11 2.0 2 0.75
12 4.5 2 1.00
13 7.0 2 0.80
14 8.0 2 0.20
15 9.0 2 0.60
Is their any graph package can do that. I would like welcome your innovative idea will be highly appreciated. I believe base R graphics or ggplot2 are flexible (but do not how to ) to do several type of graphs.
here is an example using ggplot2:
# top panel
ggplot(mydf, aes(position, factor(group), size = barheight)) +
geom_point() + opts(legend.position = "none")
# bottom panel
ggplot(mydf, aes(y = factor(group),
xmin = position - 0.1,
xmax = position + 0.1,
ymin = group - barheight/2,
ymax = group + barheight/2)) +
geom_rect()
UPDATE
here is an example with the horizontal bar:
# arbitral bar length
bar <- data.frame(y = c(1, 1, 2, 2), x = c(0, 10, 1, 9))
ggplot() +
geom_line(aes(x, factor(y), group = factor(y)),
bar, size = 2, colour = "skyblue") +
geom_rect(aes(y = factor(group),
xmin = position - 0.1,
xmax = position + 0.1,
ymin = group - barheight/2,
ymax = group + barheight/2),
mydf)
# bar length is from data range
ggplot(mydf) +
geom_line(aes(position, factor(group), group = factor(group)),
size = 2, colour = "skyblue") +
geom_rect(aes(y = factor(group),
xmin = position - 0.1,
xmax = position + 0.1,
ymin = group - barheight/2,
ymax = group + barheight/2))
UPDATED AGAIN
I should have used geom_tile
:
ggplot(mydf, aes(position, factor(group), group = factor(group))) +
geom_line(size = 2, colour = "skyblue") +
geom_tile(aes(height = barheight))
UPDATED AGAIN
ggplot(mydf, aes(position, factor(group), group = factor(group))) +
geom_line(size = 2, colour = "skyblue") +
geom_tile(aes(height = barheight)) +
geom_point(aes(x, y, group = NULL), data.frame(x = c(5, 5), y = c(1, 2)),
size = 5, colour = "cyan")
Using very basic commands can give you more control over the layout and make things much more tidy in terms of graphical layout. In my approach I only use fields
package to make horizontal lines, the rest is done with basic commands from graphics
:
#Create example data with coordinates for plotting height of bars
position <- c(0, 1, 3, 4, 5, 7, 8, 9, 0, 1, 2, 4.5, 7, 8, 9)
group <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2)
barheight <- c(0.5, 0.4, 0.4, 0.4, 0.6, 0.3, 0.4, 1, 0.75, 0.75, 0.75, 1, 0.8, 0.2, 0.6)
y.start <- c(group-barheight/2)
y.end <- c(group+barheight/2)
mydf <- data.frame (position, group, barheight, y.start, y.end)
#Remove any crap from the plot
plot(0,type="n",ylim=c(0,3),xlim=c(0,10),axes=F,ylab="",xlab="")
#Create two horizontal lines
require(fields)
yline(1,lwd=4)
yline(2,lwd=4)
#Create text for the lines
text(10,1.1,"Group 1",cex=0.7)
text(10,2.1,"Group 2",cex=0.7)
#Draw vertical bars
segments(mydf$position[1:8],mydf$y.start[1:8],y1=mydf$y.end[1:8])
segments(mydf$position[9:15],mydf$y.start[9:15],y1=mydf$y.end[9:15])
#Add circle in custom position
require(plotrix)
draw.circle(mydf$position[14],2,0.2)
draw.circle(mydf$position[4],1,0.2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With