我试图在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) …是否可以在图中着色部分标题?
x = 1:10
y = 1:10
plot(x, y, main="title (slope=1)")
在这个情节中,我想将颜色slope=1改为红色.
我试图实现多色文本,如下所示:
引用了这个:
这就是我提出的(在这里得到帮助):
require(ggplot2)
require(grid)
png(file="multicolortitle.png",width=800,height=500)
qplot(x = hp,y = mpg,data = mtcars,color=factor(mtcars$cyl),size=2) +
  scale_colour_manual(values = c("red3","green3","blue3")) + 
  theme_bw() +
  opts(title = " \n ") +
  opts(legend.position = "none") 
spacing  <- 20
grid.text(0.5, unit(1,"npc") - unit(1,"line"), 
          label=paste("4 cylinder,",paste(rep(" ",spacing*2), collapse='')),
          gp=gpar(col="red3", fontsize=16,fontface="bold"))
grid.text(0.5, unit(1,"npc") - unit(1,"line"), 
          label=paste(paste(rep(" ",spacing), collapse=''),"6 cylinder,",
            paste(rep(" ",spacing), collapse='')),
          gp=gpar(col="green3", fontsize=16,fontface="bold"))
grid.text(0.5, unit(1,"npc") - unit(1,"line"), 
          label=paste(paste(rep(" ",spacing*2), collapse=''),"8 cylinder"),
          gp=gpar(col="blue3", fontsize=16,fontface="bold"))
grid.text(0.5, unit(1,"npc") - unit(2,"line"), 
          label=paste(paste(rep(" ",spacing*0), collapse=''),
            "- Horsepower versus Miles …如何在ggplot中为轴标签设置多种颜色?
作为示例,我希望y轴标签为红色和绿色,而不是图例,而不是图例,以对应下图中的不同点:
p <- ggplot(mpg[mpg$model=="a4",],aes(x=trans,y=cty))+geom_point(color="red")+
     geom_point(aes(y=hwy),color="dark green") +
     ylab("MPG (city); MPG (hwy)")
我知道我可以使用主题控制整个y轴标签的颜色,如下所示:
p <- p + theme(axis.title.y = element_text(color="red"))
但是,在情节中我希望"MPG(hwy)"呈深绿色.有没有办法在ggplot中这样做?
这是一个情节:
library(ggplot2)
library(tibble)
ggplot(head(mtcars) %>% rownames_to_column("cars"),
       aes(x = reorder(cars, - drat), 
           y = drat)) +
  geom_col() +
  coord_flip()
如何在特定汽车名称上应用粗体(例如仅在“Hornet 4 Drive”和“Datsun 710”上)?
我更喜欢一个非常“通用”的答案,即一个可以轻松应用特定颜色或其他字体系列而不是粗体的答案。
我想在图表的标题中为某些单词添加颜色.我在这里找到了一些先例.具体来说,我希望包含在撇号(在下面的输出中)的文本对应于各自条形图的颜色.
在将PDF导出到Adobe Illustrator或其他程序之前,我已经获得了R中的标题.
name <- c("Peter", "Gabriel", "Rachel", "Bradley")
age <- c(34, 13, 28, 0.9)
fake_graph <- family[order(family$age, decreasing = F), ]
fake_graph <- within(fake_graph, {
    bar_color = ifelse(fake_graph$name == "Rachel", "blue", "gray")
})
# Plot creation
library(ggplot2)
fake_bar_charts <- ggplot() +
  geom_bar(
    data = fake_graph,
    position = "identity",
    stat = "identity",
    width = 0.75,
    fill = fake_graph$bar_color,
    aes(x = name, y = age)
    ) +
  scale_x_discrete(limits = fake_graph$name) +
  scale_y_continuous(expand = c(0, 0)) +
  coord_flip() …请考虑以下图表:
require(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
  labs(title = 'Iris[small font]' ) +
  theme_classic()
左图是代码输出,右图显示了所需的结果,我使用了Adobe Illustrator
问题是,如果可以在行中更改字体大小,在本例中标题中的"[small font]"标签,但当然这也是关于其他标签的一般性问题,例如轴和图例等等
显然,字体大小被设定为theme().但是,可能有一种方法设置"相对字体大小",例如使用rel()和以某种方式使用贴标机功能?