相关疑难解决方法(0)

ggplot2 - 两条垂直线之间的阴影区域

ggplot2用来创建一些相当简单的散点图.我目前使用两条简单的垂直线:

... + geom_vline(xintercept=159684.186,linetype="dotted",size=0.6)+
geom_vline(xintercept=159683.438,linetype="dotted",size=0.6)+ ...
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何遮蔽从Y轴顶部到X轴这两条线之间的区域?

r ggplot2

31
推荐指数
2
解决办法
3万
查看次数

如何更改单个facet_wrap面板的格式?

是否有可能改变单个分面图的格式?例如,使用下面的示例代码,可以更改cyl = 8图表的标题或背景颜色吗?

library(ggplot2)
ggplot(mtcars, aes(x=gear)) + 
  geom_bar(aes(y=gear), stat="identity", position="dodge") +
  facet_wrap(~cyl)
Run Code Online (Sandbox Code Playgroud)

r ggplot2

10
推荐指数
2
解决办法
4963
查看次数

如何在ggplot命令中激活两个不同的scale_fill_manual

这个问题来自我之前关于ggplot2中背景颜色的问题.

从那里的答案,我现在可以用来geom_rect为我的情节提供五种不同颜色的背景.最重要的是,我想绘制一个使用两种不同颜色的条形图.我可以单独完成这些任务,但是当我尝试将它们组合起来时,scale_fill_manual命令会发生冲突.

这是我正在尝试的:

scores = data.frame(category = 1:4, percentage = c(34,62,41,44), type = c("a","a","a","b"))
rects <- data.frame(ystart = c(0,25,45,65,85), yend = c(25,45,65,85,100), col = letters[1:5])
labels = c("ER", "OP", "PAE", "Overall")
medals = c("navy","goldenrod4","darkgrey","gold","cadetblue1")

ggplot() + 
geom_rect(data = rects, aes(xmin = -Inf, xmax = Inf, ymin = ystart, ymax = yend, fill=col), alpha = 0.3) + 
scale_fill_manual(values=medals) +
opts(legend.position="none") + 
geom_bar(data=scores, aes(x=category, y=percentage, fill=type), stat="identity") +
#scale_fill_manual(values = c("indianred1", "indianred4")) +
scale_x_continuous(breaks = 1:4, …
Run Code Online (Sandbox Code Playgroud)

r graph ggplot2

8
推荐指数
1
解决办法
5187
查看次数

自定义背景以突出显示ggplot中的数据范围

我想在我的ggplot中设置背景颜色以突出显示数据范围.特别是,我想[-0.1,0.1]用绿色[-0.25,-0.1)(0.1,0.25]橙色突出显示.换句话说,我需要的是具有一些alpha透明度的条形图,其y-限制是图形的y范围,x-limits由我设置.

理想情况下,我会想要一些不敏感的东西coord_cartesian(...)(如设置vline(...,size = X)那样).另外,拥有独立于任何数据的东西并且仅基于绘图坐标会很好.我试过geom_segment,但我无法想象我们如何设置一个可行的宽度.

library(ggplot2)
x <- c(seq(-1, 1, by = .001))
y <- rnorm(length(x))
df <- as.data.frame(x=x,y=y)

ggplot(df,aes(x,y)) +
  geom_point(aes(y*abs(x)),alpha=.2,size=5) +
  theme_bw() +
  coord_cartesian(xlim = c(-.5,.5),ylim=c(-1,1))
Run Code Online (Sandbox Code Playgroud)

例

r ggplot2

8
推荐指数
2
解决办法
3595
查看次数

仅在图表的一个区域上更改背景颜色

我想改变图表的一部分的背景颜色.那可能吗?

例如,使用下面的图表我可能想表明我对重量在2到4吨之间的汽车特别感兴趣,因此希望使用粉红色背景突出显示该区域.

更具体地说,我想覆盖一个透明的粉红色矩形,它在x轴上从2伸展到4,覆盖整个y轴区域.

我该如何编码呢?

p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
Run Code Online (Sandbox Code Playgroud)

background r ggplot2

8
推荐指数
2
解决办法
7817
查看次数

在x轴上的日期之间提供阴影

我在R中有一个图,X轴上有日期/时间(POSIXct),Y轴有一些数据.
我希望在x轴上每个日期的下午3点到6点之间提供x轴上的阴影

r ggplot2

7
推荐指数
2
解决办法
7706
查看次数

如何着色绘图子区域并使用ggrepel标记数据点的子集?

我制作了这个火山阴谋,并希望改进它如下:

制作了这个火山的情节

  1. 用蓝色数据点完全遮蔽该区域:使用我当前的代码,我无法将阴影扩展到您所看到的范围之外.我希望它一直到剧情区域限制.

  2. geom_text让我来标记数据点的子集,但做起来ggrepel应该添加有标签从而提高标签的清晰度连接数据点的线.如何重用现有geom_text代码ggrepel来实现这一目标?

这是我的代码:

ggplot(vol.new, aes(x = log2.fold.change, y = X.NAME., fill = Color)) + # Define data frame to be used for plotting; define data for x and y axes; crate a scatterplot object.

  geom_point(size = 2, shape = 21, colour = "black") + # Define data point style.

  ggtitle(main.title, subtitle = "Just a little subtitle") + # Define title and subtitle.

  labs(x = x.lab, y = y.lab) + # Define labels …
Run Code Online (Sandbox Code Playgroud)

r bioinformatics ggplot2 ggrepel

7
推荐指数
1
解决办法
450
查看次数

使用因子变量更改 ggplot 图表的背景颜色

ggplot在 R 中使用来绘制一些数据。我想做的是绘制一个散点图,其中图表的不同区域的背景不同。这里有一个非常有用的答案,它让我得到了大部分的帮助,但不是全部。

这是数据示例

row.names selectionDirector country     Totals    director.rank
1   268   Alfred Hitchcock  Argentina   14        1
2   269   Alfred Hitchcock  Australia   7         3
3   274   Alfred Hitchcock  Canada      10        1
4   286   Alfred Hitchcock  France      18        1
5   288   Alfred Hitchcock  Germany     9         6
6   296   Alfred Hitchcock  Italy       5         3
7   319   Alfred Hitchcock  Spain       21        4
8   320   Alfred Hitchcock  Sweden      4         8
9   325   Alfred Hitchcock  UK          87        1
10  330   Alfred Hitchcock …
Run Code Online (Sandbox Code Playgroud)

r data-visualization ggplot2

6
推荐指数
1
解决办法
2万
查看次数

将ggplot中背景的部分更改为不同的颜色

使用以下数据框d:

day <- gl(8,1,24,labels=c("Mon","Tues","Wed","Thurs","Fri","Sat","Sun","Avg"))
day <- factor(day, level=c("Mon","Tues","Wed","Thurs","Fri","Sat","Sun","Avg"))
month<-gl(3,8,24,labels=c("Jan","Feb","Mar"))
month<-factor(month,level=c("Jan","Feb","Mar"))
snow<-gl(1,24,labels=c("Y"))
snow<-factor(snow,levels=c("Y","N"))
count <- c(4,5,6,8,3,4,9,5.57,2,4,3,7,1,9,3,4.14,7,9,6,3,1,2,8,5.14)
d <- data.frame(day=day,count=count,month=month,snow=snow)
Run Code Online (Sandbox Code Playgroud)

折线图背后的背景颜色与条形图相同:

ggplot(data=d[d$day=="Avg",], aes(x=day , y=count, fill=month)) + 
geom_bar(position = "dodge", width = 1.2, stat="identity") +
geom_text(aes(label=month, x=day, y=count), position=position_dodge(width=1.2), vjust=-.6, size=3) + 
geom_line(data=d[d$day!="Avg",], aes(x=day, y=count, group=month, colour=month)) + 
facet_wrap(~snow,ncol=1,scales="free") + 
scale_x_discrete(limits=levels(d$day))
Run Code Online (Sandbox Code Playgroud)

是否可以更改条形图后面部分的背景颜色?

没有部分背景颜色变化的图形 在此输入图像描述

background r graph colors ggplot2

5
推荐指数
1
解决办法
7338
查看次数

未找到geom_rect()主变量

我想用我的ggplot绘制矩形"阴影".ggplot代码工作并提供如下所示的图像.我在这里查找信息并构建了x和y值的数据框.

mydf<-data.frame(tiempo=df5$tiempo,vel=df5$TR2x45.17)[1:14,]
structure(list(tiempo = c(618.2, 618.4, 618.6, 618.8, 619, 619.2, 
619.4, 619.6, 619.8, 620, 620.2, 620.4, 620.6, 620.8), vel = c(0, 
0, -4, -9, 5, 9, 1, 4, 0, 0, -1, -4, 0, 1)), .Names = c("tiempo", 
"vel"), row.names = c(NA, 14L), class = "data.frame")

rects <- data.frame(xstart = seq(618,619.5,.5), xend = seq(618.5,620,.5), col = letters[1:4])

ggplot(data=mydf,
       aes(x=tiempo,y=vel))+theme_minimal()+
           geom_point(size=4)+
    labs(title=c("Velocidad ejemplo pasaje figura"))+
       geom_smooth(method="loess", span=.3, se=FALSE, colour="red", size=1,alpha=0.5) +
geom_rect(data = rects, aes(xmin = xstart, xmax = xend, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
270
查看次数

R:如何更改ggplot中的配色方案(需要14种颜色)

我使用以下代码使用ggplot创建一个图:

m = ggplot(derv, aes(x=Date, y=derv, colour = Season)) + geom_point() 
m2 = m+geom_abline(intercept = 0, slope = 0)
m3 = m2 + geom_abline(intercept = 2.578269274, slope = 0)
m3 = m3 + geom_abline(intercept = -1.4242559021, slope = 0) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这个情节看起来很漂亮但是对于某些间隔,例如2010sp和2010au,我很难分辨出颜色何时发生变化.所以我想改变这个情节的配色方案.

我尝试了以下代码:

 m3+scale_color_brewer(palette="Dark2")
Run Code Online (Sandbox Code Playgroud)

但我收到一条警告信息:

在此输入图像描述

2: In RColorBrewer::brewer.pal(n, pal) :
n too large, allowed maximum for palette Dark2 is 8
Returning the palette you asked for with that many colors
Run Code Online (Sandbox Code Playgroud)

我检查了可用的调色板,最大的一个包含12种颜色,但我需要14种颜色,所以我想知道是否有办法解决这个问题.

graphics r ggplot2

4
推荐指数
1
解决办法
6581
查看次数

根据数据集中的另一个变量设置背景颜色

我试图展示春分对我的数据的影响,这是一整年的一系列远程跟踪鸟类。为了做到这一点,我想绘制一年中每一天的纬度,并将春分效果作为背景中的颜色分级。

我的数据看起来像这样:

SO <- data.frame(date = seq(as.Date("2000/1/1"), by = "day", length.out = 365),
                 latitude = cumsum(rnorm(365)),
                 eqx.effect = c(rep(0,60),seq(1,20,1), seq(20,1,-1),rep(0,143),seq(1,20,1), seq(20,1,-1),rep(0,82)),
                 location = c(rep(1,100),rep(2,135), rep(3,130)))     
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经设法用 geom_line 为几只鸟绘制了全年的纬度,并根据它们所在的位置用不同的颜色为线条的不同部分着色。为了更改背景,我已经阅读了在 R使用ggplot2 中使用 geom_rect 的信息,如何使图形的背景在不同区域具有不同颜色?但在那个问题中,这个人只需要一堆矩形,而我需要 365。

有谁知道这样做的任何其他方式?如果我能让默认背景在接近春分点的日期更加透明,那也会很有用。

background r ggplot2

2
推荐指数
1
解决办法
1251
查看次数

在ggplotly线图中填充背景间隔

我正在尝试填充折线图的背景ggplot以指示白天/夜间时段。这个答案中的方法效果很好,但我想使用交互式地显示它们ggplotly,并且由于这个错误而成为一个问题,其中 ggplotly 不喜欢 -Inf 和 Inf 用作 y 限制geom_rect。有谁知道可以与 ggplotly 一起使用的解决方法吗?

为了便于阅读,我将其他答案中的示例代码粘贴到此处:

library(ggplot2)
dat <- data.frame(x = 1:100, y = cumsum(rnorm(100)))
#Breaks for background rectangles
rects <- data.frame(xstart = seq(0,80,20), xend = seq(20,100,20), col = letters[1:5])

p <- ggplot() + 
  geom_rect(data = rects, aes(xmin = xstart, xmax = xend, ymin = -Inf, ymax = Inf, fill = col), alpha = 0.4) +
  geom_line(data = dat, aes(x,y))
p
Run Code Online (Sandbox Code Playgroud)

产生这个可爱的人物: 具有填充背景的 ggplot 线图

但是,如果您这样做:

ggplotly(p) …
Run Code Online (Sandbox Code Playgroud)

background r fill ggplot2 plotly

2
推荐指数
1
解决办法
375
查看次数