我试图在ggplot2中创建的图表下面显示一些有关数据的信息.我想使用图的X轴坐标绘制N变量,但Y坐标需要距离屏幕底部10%.实际上,期望的Y坐标已经作为y_pos变量存在于数据框中.
我可以想到使用ggplot2的3种方法:
1)在实际绘图下方创建一个空图,使用相同的比例,然后使用geom_text在空白图上绘制数据.这种方法有点有效,但非常复杂.
2)geom_text用于绘制数据,但以某种方式使用y坐标作为屏幕的百分比(10%).这将强制数字显示在图表下方.我无法弄清楚正确的语法.
3)使用grid.text显示文本.我可以轻松地将它设置在屏幕底部的10%,但我无法确定如何设置X coordindate以匹配绘图.我试图使用grconvert捕获最初的X位置但是也无法使其工作.
以下是虚拟数据的基本情节:
graphics.off() # close graphics windows
library(car)
library(ggplot2) #load ggplot
library(gridExtra) #load Grid
library(RGraphics) # support of the "R graphics" book, on CRAN
#create dummy data
test= data.frame(
Group = c("A", "B", "A","B", "A", "B"),
x = c(1 ,1,2,2,3,3 ),
y = c(33,25,27,36,43,25),
n=c(71,55,65,58,65,58),
y_pos=c(9,6,9,6,9,6)
)
#create ggplot
p1 <- qplot(x, y, data=test, colour=Group) +
ylab("Mean change from baseline") +
geom_line()+
scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) …Run Code Online (Sandbox Code Playgroud) 我用ggplot2创建了这个情节:

外线需要对应Y刻度(即Text1的线的Y位置应为100和85).我可以通过在条形图右侧绘制一个空白绘图并使用与条形图相同的比例然后使用注释函数绘制线来实现此目的.另一种方法是简单地"手动"绘制线条grid.lines,但是坐标grid.lines不对应于图形的Y标度.
是否有可能以某种方式使用不同的方法绘制这些线?我认为必须完成它grid.lines.我怎么能把条形图的Y coordindates传递给grid.lines?
下面是用于创建此图的最小代码:
library (ggplot2)
test= data.frame(
group=c(rep(1,6), rep(2,6)),
subgroup=c( 1,1,1,2,2,2,1,1,1,2,2,2),
category=c( rep(1:3, 4)),
count=c( 10,80,10,5,90,5, 10,80,10,5,90,5 )
)
qplot(subgroup,
count,
data=test,
geom="bar",
stat="identity",
fill =category,
facets = .~ group, width=0.9)+
opts(legend.position="none",
plot.margin = unit(c(0,9,2,0), "lines"))
Run Code Online (Sandbox Code Playgroud)

如何画出条形图右侧的线条?
我最近在ggplot2中问了一个关于在绘图区域之外绘制文本的问题,解决方案是使用gt$layout和grid.draw.
可以在这里使用类似的方法吗?据我所知,annotation_custom仅适用于文本,不适用于其他图形元素.谢谢
我需要使用以下数据创建一个包含组的简单线图:
test = data.frame(x = rep(1:3, each = 2),
group = rep(c("Group 1","Group 2"),3),
groupcd= rep(c(1,2),3),
y= c(22,8,11,4,7,5)
)
Run Code Online (Sandbox Code Playgroud)
我可以用GGPLOT轻松完成:
library(ggplot2)
#GGPLOT
qplot(x=x, y=y,
data=test,
colour=group,
main="GGPLOT line plot with groups") +
geom_line()
Run Code Online (Sandbox Code Playgroud)

我也可以用TRELLIS做到:
library(lattice)
xyplot(y~x,
type="b",
group=group,
data=test,
main="TRELLIS line plot with groups",
auto.key =list(
lines = TRUE)
)
Run Code Online (Sandbox Code Playgroud)

但是,我现在有点不愿意使用GGPLOT或TRELLIS.我希望能够用Base R创建这个图.我可以让这个图在Base R中工作的唯一方法是使用for循环:
# set up empty plot
plot(test$y ~test$x, ylab="y", xlab="x", type="n", main="Base R line plot with groups")
colors<-c("red","blue")
#plot each group in the for loop
number_of_groups <- as.numeric(max(unique(test$groupcd))) …Run Code Online (Sandbox Code Playgroud) 看起来条带总是在ggplot2创建的图上方.他们可以移到情节之下吗?
例如:
library(ggplot2)
qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer)
Run Code Online (Sandbox Code Playgroud)
在顶部显示汽车信息.它们可以显示在底部吗?
我需要制作一系列水平分组条形图.条形图功能不会自动调整绘图的边距,因此文本会被切断.
graphics.off() # close graphics windows
test <- matrix(c(55,65,30, 40,70,55,75,6,49,45,34,20),
nrow =3 ,
ncol=4,
byrow=TRUE,
dimnames = list(c("Subgroup 1", "Subgroup 2", "Subgroup 3"),
c(
"Category 1 Long text",
"Category 2 very Long text",
"Category 3 short text",
"Category 4 very short text"
)))
barplot(test,
las=2,
beside = TRUE,
legend=T,
horiz=T)
Run Code Online (Sandbox Code Playgroud)

我找不到一个选项来自动向右移动图,R点图功能的方式((SAS中的条形图程序也会自动调整边距).显然,我总是可以使用par函数手动调整边距.
par(mar=c(5.1, 13 ,4.1 ,2.1))
Run Code Online (Sandbox Code Playgroud)
将剧情向右移动

是否可以根据文本的长度自动将图形向右移动(即调整边距)?
我可以想到2个相关的appproaches以编程方式进行:1)计算最长文本字符串的长度并相应地调整左边距2)创建数据的点图,以某种方式捕获边距并在条形图中使用相同的边距.
有更简单的方法吗?谢谢!
我需要找到一种方法,以与参考线本身相同的角度注释参考线.
以下语句将生成参考线及其上方的标签.但是,线的斜率可能会发生变化,我需要找到一种方法来确保注释始终处于相同的角度.
plot(1:10,1:10)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=-28)
Run Code Online (Sandbox Code Playgroud)

在R中有一个简单的方法吗?提前致谢
我需要根据EXIF数据操作一些图像文件.可以在R中完成这样的事情吗?我还没有看到任何可以直接读取EXIF数据的R函数.
我想弄清楚如何在ggplot中使用grconvertX/grconvertX.我的最终目标是使用和从用户坐标到设备坐标向ggplot2图形(可能是晶格)添加注释.我知道它可以用grobs完成,但我想知道是否有更简单的方法.grid.textgrid.lines
以下代码允许我将值从用户坐标传递到ndc坐标,并使用这些值来注释绘图grid.text.
graphics.off() # close graphics windows
library(grid)
library(gridBase)
test= data.frame(
x = c(1,2,3),
y = c(12,10,3),
n = c(75,76,73)
)
par(mar = c(13,5,2,3))
plot(test$y ~ test$x,type="b", ann=F)
for (i in 1:nrow(test))
{
X=grconvertX(i , from="user", to="ndc")
grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5)) ) )
grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}
#add some code to save as PDF ...
Run Code Online (Sandbox Code Playgroud)

该代码基于我以前的一篇文章的解决方案:混合X和Y坐标系.您可以看到原始图中的x坐标是如何转换为ndc的.这种方法的优点是我可以使用Y的设备坐标.
我假设我可以轻松地在ggplot2(也可能在 …
我试图弄清楚如何确定分组条的中点值,即每个条的中心的实际X位置.这是很容易在基础R完成barplot功能,但是我希望能够做到这一点的格子的barchart.我的目标是在相应的栏顶部显示文本列的值.
只要我不使用子组,下面的代码允许我将文本放在条形图的顶部.我试过在互联网上搜索解决方案,但似乎没有任何效果.从图中可以看出,中点仅针对整个组的中心确定.
谢谢!
library(lattice)
test= data.frame(
group=c("WK 1", "WK 1", "WK 1", "WK 2", "WK 2", "WK 2", "WK 3", "WK 3", "WK 3"),
subgroup=c(1,2,3,1,2,3,1,2,3) ,
percent=c(60,50,80,55,56,65,77,65,86),
text=c("n=33", "n=37","n=39","n=25","n=27","n=22","n=13","n=16","n=11")
)
barchart(data=test,
percent~group,
groups=subgroup,
panel = function(x,y,...){
panel.barchart(x, y, ...)
panel.text( x=unique(test$group),
y=test$percent,
label=unique(test$text)
)
}
)
Run Code Online (Sandbox Code Playgroud)

我需要找到一种在文字下方1英寸处放置文字的方法.即使我更改了绘图的边距或使用不同的数据,我也需要将该文本放在绘图下方1英寸处.我一直在调用带有修改的x和y值的网格文本,但我想要一些能够适应ggplot维度的东西.另一种方法是使用grobs(参见相关文章),但这需要根据数据设置Y位置.
这是基本代码:
library(ggplot2)
test= data.frame(
x = c(1:10 ),
y = c(1:10)
)
qplot(x=x, y=y, data=test)+
opts(plot.margin = unit(c(1,3,8,1), "lines")) +
geom_line()
Run Code Online (Sandbox Code Playgroud)

谢谢.
这段代码
library(ggplot2)
test=read.table(text=
"group fillcd percent
1 1 73.2
1 2 73.8
1 3 78.6
2 1 78.1
2 2 95.6
2 3 95.5
", header=TRUE, sep="" )
test$fill <- factor(test$fillcd, labels=c("XX", "EE", "BB"))
test$text=paste(test$percent,"%")
ggplot(data=test,
aes(group, percent, fill=fill)) +
geom_bar(stat="identity",position="dodge")+
coord_flip()+
geom_text(data = test, aes(y = percent, x = group, label = text ))
Run Code Online (Sandbox Code Playgroud)
生成以下图表:

如何获得条形的中点以在那里放置标签?