可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
3 plots arranged in 2 rows and 2 columns
attach(mtcars) par(mfrow=c(2,2)) plot(wt,mpg, main="Scatterplot of wt vs. mpg") plot(wt,disp, main="Scatterplot of wt vs disp") boxplot(wt, main="Boxplot of wt")
But how do I create a panel with just text and then add it to the fourth position in the grid. Apologies. I see now that like many quesioners I was asking you to read my mind or intuit from my code that I expected the text to be arranged left-justified in a grid of locations in the blank space.
(For some reason this seemed perfectly reasonable to ask but it was deleted, despite no close votes or nomination for duplicates offered. I'll put in what I had cobbled together, as an answer but there might be more elegant solutions. I did look at the titles for nominations of similar questions, but didn't find any that matched this problem.)
plot(1:100, 1:100, axes=FALSE, ylab="", xlab="", type="n") text( rep(c(1,50), each=10), rep( seq(1, 100, by=10),2), labels=letters[1:20])
I've wondered after the fact whether a fine grained grid was the right way to do it. Perhaps a coarser grid with just the right number of rows and columns and using left justified text would be more table-like. The gridExtra package with baptiste's tableGrob might also be a productive direction.
An alternate text argument to "plot" might be:
回答1:
Here's one example,
library(gplots) textplot(txt)
There's also addtable2plot
in plotrix
, and an alternative solution would be to mix base and grid graphics with the gridBase package, and place a tableGrob
from the gridExtra
package.
回答2:
Thought I'd throw in my two cents here. I see two simple ways to do this (granted neither shows row and column indices, as currently configured). Both use text
as you've proposed, but work to layout things in the coarser grid you discuss but don't show. For left-alignment in both the key argument is pos
.
Option 1 (uses linebreaks to create the grid)
attach(mtcars) par(mfrow=c(2,2)) plot(wt,mpg, main="Scatterplot of wt vs. mpg") plot(wt,disp, main="Scatterplot of wt vs disp") boxplot(wt, main="Boxplot of wt") txt <- structure(c("am", "carb", "cyl", "disp", "drat", "gear", "hp", "mpg", "qsec", "vs", "wt", "this"), .Dim = 3:4) plot.new() sapply(1:4, function(i) text(i/4, .5, paste(txt[,i],collapse='\n'), pos=4))
Option 2 (uses text vectorization to create the grid)
attach(mtcars) par(mfrow=c(2,2)) plot(wt,mpg, main="Scatterplot of wt vs. mpg") plot(wt,disp, main="Scatterplot of wt vs disp") boxplot(wt, main="Boxplot of wt") txt <- structure(c("am", "carb", "cyl", "disp", "drat", "gear", "hp", "mpg", "qsec", "vs", "wt", "this"), .Dim = 3:4) plot.new() sapply(1:4, function(i) text(i/4, (1:3)/3, rev(txt[,i]), pos=4))
回答3:
Not sure what text you want, but here's a "hello world":
plot.new() text(0.5,0.5,"hello world",cex=6)