r-grid

How to apply cross-hatching to a polygon using the grid graphical system?

怎甘沉沦 提交于 2019-11-27 22:51:16
Several functions in R's base graphical system, including rect() and polygon() , support cross-hatching via their angle= and density= arguments: x = c(0, 0.5, 1, 0.5) y = c(0.5, 1, 0.5, 0) par(mar=c(0,0,0,0)) plot.new() polygon(x, y, angle=45, density=10) How might I apply similar cross-hatching to a polygon drawn by the grid graphical system's grid.polygon() function: library(grid) grid.newpage() grid.polygon(x,y) I've looked in the documentation for ?grid.polygon and ?gpar , and have skimmed through Paul Murrel's book on R graphics, and have so far come up empty. Am I missing something

Change text color for cells using TableGrob

我与影子孤独终老i 提交于 2019-11-27 22:26:10
Is there a way to individually change the color of the text of a cell when using tableGrob and ggplot2? For instance in the code below it'd be great if the cell with 1 could be blue and the cell with 2 could be red, with 3:8 all black. library(ggplot2) library(grid) mytable = as.table(matrix(c("1","2","3","4","5","6","7","8"),ncol=2,byrow=TRUE)) mytable = tableGrob(mytable,gpar.coretext = gpar(col = "black", cex = 1)) mydf = data.frame(x = 1:10,y = 1:10) ggplot( mydf, aes(x, y)) + annotation_custom(mytable) Much to my disappointment, this does not seem to be easy. The tableGrob function calls

Justification of multiple legends in ggmap/ggplot2

萝らか妹 提交于 2019-11-27 22:05:18
I am trying to make a map with two legends denoting shape and colour ("Type" and "Org" in the example below), and have the legends inset. I can place the legends, but I would like them to be left justified so that their left edges line up. I can't make them anything other than centred with respect to each other: require(ggplot2) require(ggmap) require(grid) require(mapproj) data <- data.frame(Org=rep(c("ABCDEFG","HIJKLMNOP","QRSTUVWX"),4) , Type=rep(c("Y","Z"),6), Lat=runif(12,48,54.5) , Long=runif(12,-133.5,-122.5)) osmMap <- get_map(location=c(-134,47.5,-122,55), source = 'osm') points <-

independently move 2 legends ggplot2 on a map

。_饼干妹妹 提交于 2019-11-27 18:46:40
问题 I want to independently move two legends on a map to save save and make the presentation nicer. Here is the data: ## INST..SUB.TYPE.DESCRIPTION Enrollment lat lng ## 1 CHARTER SCHOOL 274 42.66439 -73.76993 ## 2 PUBLIC SCHOOL CENTRAL 525 42.62502 -74.13756 ## 3 PUBLIC SCHOOL CENTRAL HIGH SCHOOL NA 40.67473 -73.69987 ## 4 PUBLIC SCHOOL CITY 328 42.68278 -73.80083 ## 5 PUBLIC SCHOOL CITY CENTRAL 288 42.15746 -78.74158 ## 6 PUBLIC SCHOOL COMMON NA 43.73225 -74.73682 ## 7 PUBLIC SCHOOL INDEPENDENT

How to manage the t, b, l, r coordinates of gtable() to plot the secondary y-axis's labels and tick marks properly

被刻印的时光 ゝ 提交于 2019-11-27 11:50:02
问题 I am using facet_wrap and was also able to plot the secondary y-axis. However the labels are not getting plotted near the axis, rather they are plotted very far. I realise it all will get resolved if I understand how to manipulate the coordinate system of the gtable (t,b,l,r) of the grobs. Could someone explain how and what they actually depict - t:r = c(4,8,4,4) means what. There are many links for secondary yaxis with ggplot, however when nrow/ncol is more than 1, they fails. So please

Make a rectangular legend, with rows and columns labeled, in grid

喜你入骨 提交于 2019-11-27 08:23:42
I've got a ggplot where I'm mapping factors to both fill and alpha, like this: set.seed(47) the_data <- data.frame(value = rpois(6, lambda=20), cat1 = rep(c("A", "B"), each = 3), cat2 = rep(c("X", "Y", "Z"), 2)) ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) + geom_bar(stat = "identity", position = "dodge") + scale_alpha_discrete(range = c(0.5, 1)) + theme_bw() The people I'm producing it for don't find the legend for alpha very clear. I think a good alternative would be something like this (which I hacked together in base graphics): I know I can't generate a legend like

How to annotate ggplot2 qplot outside of legend and plotarea? (similar to mtext())

半城伤御伤魂 提交于 2019-11-27 06:50:23
问题 I would like to annotate my plots with a filename. With plot() I used mtext : plot(1:10) mtext("File xy-12-34-56.csv", 4) How can I do that with ggplot2 and qplot or ggplot? It should not collide with the legend. I found the commands annotate and grid , but I could not get an annotation similar to mtext with these. As a workaround I could try watermarks, but perhaps you have a good hint for me. Kind regards, Jonas 回答1: Update Looks like to achieve the result now we should use the following:

Grid of multiple ggplot2 plots which have been made in a for loop

ぃ、小莉子 提交于 2019-11-27 06:45:39
as a new ggplot2 user, I am a bit lost with the amount of possibilities, and struggle to find on the net a simple answer to what I consider a simple problem. I would like to display multiple plots from ggplot2 on a same sheet, BUT knowing that these plots come from a for loop. Following example does not compile, it is only to illustrate : for(i in c(1:n)){ for(j in c(1:m)){ .......... # some data production p <- ggplot(df.all) + geom_bar(aes_string(x=class.names[i],fill=var.names[j]) }} Here, p is overwritten, but I would like to have instead a matrix or a list in which I can put all the p as

Align grob at fixed top/center position, regardless of size

China☆狼群 提交于 2019-11-27 06:26:37
问题 I have some table grobs, some long and some short. I'd like to draw these at the top/center of the page (with a small margin). From this answer, I got a helpful starting point, but the positioning is dependent on each grob's height. library(gridExtra) # fake data my_df <- data.frame(col1=rep('hello', 35), col2=round(rnorm(35), 3)) # list of grobs tg <- list(tableGrob(my_df[1:30, ]), tableGrob(my_df[31:35, ])) # this positions grobs at center top, but varies based on rows in table tg[[1]]$vp <

How to plot barchart onto ggplot2 map [duplicate]

╄→гoц情女王★ 提交于 2019-11-27 03:20:31
问题 This question already has an answer here : Plotting bar charts on map using ggplot2? (1 answer) Closed 2 years ago . I have found similar answers to questions like this one, but most of them are using packages rworldmap , ggmap , ggsubplot or geom_subplot2d . See for example here or here. I'd like to know how I can plot other ggplot-objects such as a bar-chart onto a map, that is created from a shapefile. The one I'm using can be downloaded here. EDIT As @beetroot correctly pointed out, the