The graph of my imagination in R is a map with the distance between the stripes and the height of the rod

I want to develop the following type of chart:

enter image description here

If the position determines the position of the bar (not one direction, but the direction, although the direction does not make much sense, but aesthetic to look like a map), and the height determines the height of the bar in each position. Below is the 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 

Can any graph package do this? I would like your innovative idea to be highly appreciated. I believe that the basic graphics of R or ggplot2 are flexible (but not able) to execute several types of graphics.

+4
source share
3 answers

here is an example of 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() 

enter image description here

UPDATE

here is an example with a 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)) 

enter image description here

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") 
+9
source

Using very simple commands can give you more control over the layout and make things tidier in terms of the graphic layout. In my approach, I use only the fields package to create horizontal lines, the rest are executed using the 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) 

enter image description here

+2
source

Is it close to you?

 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) library(ggplot2) ggplot(mydf, aes(position, barheight)) + geom_bar(stat = "identity") + facet_grid(group ~ .) 

enter image description here

+1
source

Source: https://habr.com/ru/post/1389276/


All Articles