相关疑难解决方法(0)

在同一图表上使用ggplot2将两个变量绘制为线条

这是一个非常新的问题,但我说有这样的数据:

test_data <-
  data.frame(
    var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
    var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
    date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
  )
Run Code Online (Sandbox Code Playgroud)

如何在x轴上使用?绘制时间序列var0var1同一图表?奖励积分,如果你做和不同的颜色,并且可以包括一个传奇!dateggplot2var0var1

我确信这很简单,但我找不到任何例子.

r graph time-series ggplot2 r-faq

293
推荐指数
5
解决办法
43万
查看次数

手动添加行时向ggplot添加图例

我在ggplot中有一个带有4个单独行的图,我添加了一个单独的geom_line()参数.我想添加图例,但scale_colour_manual在这种情况下不起作用.当我单独添加变量时添加图例的正确方法是什么?

这是我的代码:

ggplot(proba[108:140,], aes(c,four)) + 
    geom_line(linetype=1, size=0.3) + 
    scale_x_continuous(breaks=seq(110,140,5)) + 
    theme_bw() + 
    theme(axis.line = element_line(colour = "black", size=0.25),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank()) + 
    theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) + 
    theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) + 
    theme(axis.ticks=element_line(colour="black",size=0.25)) + 
    xlab("\nTime-steps") + 
    ylab("Proportion correct\n") + 
    theme(axis.text=element_text(size=8),axis.title=element_text(size=8)) + 
    geom_line(aes(c,three), size=0.2, linetype=2) + 
    geom_line(aes(c,one),linetype=3, size=0.8, colour="darkgrey") + 
    geom_line(aes(c,two), linetype=1, size=0.8, colour="darkgrey")
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

ggplot2中相同图例中的不同图例键

假设我不需要"正确的"变量映射,但仍希望使用图例键来帮助理解图表.我的实际数据类似于以下df

df <- data.frame(id = 1:10, line = rnorm(10), points = rnorm(10))

library(ggplot2)

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, colour = "points"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

基本上,我希望传说键相对于points......只是一个点,没有中间的线.我接近这个:

library(reshape2)

df <- melt(df, id.vars="id")

ggplot() +
  geom_point(aes(id, value, shape = variable), df[df$variable=="points",]) +
  geom_line(aes(id, value, colour = variable), df[df$variable=="line",])
Run Code Online (Sandbox Code Playgroud)

但它定义了两个独立的传说.修复第二个代码(并且必须重塑我的数据)也没关系,但我更喜欢一种方法(如果有的话)手动更改任何图例键(并继续使用第一个approch).谢谢!

编辑:

谢谢@alexwhan你更新了我对变量映射的记忆.但是,到目前为止我最简单的方法仍然是以下(非常糟糕的黑客!):

df <- data.frame(id = 1:10, line = rnorm(10), points = rnorm(10))

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, shape = "points")) +
  theme(legend.title=element_blank())
Run Code Online (Sandbox Code Playgroud)

这只是隐藏了两个不同 …

r ggplot2 legend-properties

16
推荐指数
2
解决办法
6806
查看次数

ggplot2:如何展示传奇

我制作了一个简单的经典情节,ggplot2其中有两个图形合二为一.但是,我正在努力展示传奇.它没有显示传奇.我没有使用融化和重塑方式,我只是使用经典的方式.以下是我的代码.

df <- read.csv("testDataFrame.csv")

graph <- ggplot(df, aes(A)) + 
  geom_line(aes(y=res1), colour="1") +
  geom_point(aes(y=res1), size=5, shape=12) +
  geom_line(aes(y=res2), colour="2") +
  geom_point(aes(y=res2), size=5, shape=20) +
  scale_colour_manual(values=c("red", "green")) +
  scale_x_discrete(name="X axis") +
  scale_y_continuous(name="Y-axis") +
  ggtitle("Test") 
  #scale_shape_discrete(name  ="results",labels=c("Res1", "Res2"),solid=TRUE) 

print(graph)
Run Code Online (Sandbox Code Playgroud)

数据框是:

 A,res1,res2
 1,11,25
 2,29,40
 3,40,42
 4,50,51
 5,66,61
 6,75,69
 7,85,75
Run Code Online (Sandbox Code Playgroud)

有关如何显示上图的图例的任何建议?

r ggplot2

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

ggplot2图例不出现的原因

我试图(不成功)在我的 R ggplot2 图中显示一个图例,它涉及多个图。我的数据框df和代码如下:

  Individuals        Mod.2        Mod.1          Mod.3
1           2 -0.013473145  0.010859793    -0.08914021
2           3 -0.011109863  0.009503278    -0.09049672
3           4 -0.006465788  0.011304668    -0.08869533
4           5  0.010536718  0.009110458    -0.09088954
5           6  0.015501212  0.005929766    -0.09407023
6           7  0.014565584  0.005530390    -0.09446961
7           8 -0.009712516  0.012234843    -0.08776516
8           9 -0.011282278  0.006569570    -0.09343043
9          10 -0.011330579  0.003505439    -0.09649456

str(df)
    'data.frame':   9 obs. of  4 variables:
     $ Individuals   : num  2 3 4 5 6 7 8 9 10
     $ Mod.2         : num  -0.01347 …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

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

为什么ggplot2图例没有显示在图中

我使用 ggplot 散点图 2 个数据集,并希望在左上角显示图例。我尝试了一些代码,但没有用。我不确定为什么会这样。

ggplot(mf, aes(log10(mf[,2]),mf[,1])) 
+ ggtitle("Plot") 
+ geom_point(color = "blue") +  theme(plot.margin = unit(c(1,2,1,1), "cm"))
+ xlab("xxx") + ylab("yyy") 
+ theme(plot.title = element_text(size=18,hjust = 0.5, vjust=4)) 
+ geom_point(data=mf2,aes(log10(mf2[,2]),mf2[,1]),color="red") 
+ theme(axis.title.x = element_text(size = rel(1.3))) 
+ theme(axis.title.y = element_text(size = rel(1.3))) 
+ scale_color_discrete(name = "Dataset",labels = c("Dataset 1", "Dataset 2"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

r legend ggplot2

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

将图例添加到多直方图ggplot中

我正在尝试向ggplot两个直方图中的一个添加图例,这些图可能会重叠,因此希望它们稍微透明:

library(ggplot2)
set.seed(1)
plot.df <- data.frame(x=c(rnorm(1000,30,1),rnorm(10000,40,5)),
                      group=c(rep("a",1000),rep("b",10000)))
Run Code Online (Sandbox Code Playgroud)

使用:

ggplot(plot.df,aes(x=x,fill=factor(group)))+ 
  geom_histogram(data=subset(plot.df,group=='a'),fill="red",alpha=0.5)+
  geom_histogram(data=subset(plot.df,group=='b'),fill="darkgray",alpha=0.5)+
  scale_colour_manual(name="group",values=c("red","darkgray"),labels=c("a","b"))+scale_fill_manual(name="group",values=c("red","darkgray"),labels=c("a","b"))
Run Code Online (Sandbox Code Playgroud)

但我得到的是:

在此处输入图片说明

少了什么东西?

r ggplot2

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

将 geom_hline 添加到图例

昨天和今天在网上搜索后,我获得图例工作的唯一方法是遵循“Brian Diggs”在这篇文章中的解决方案: 将图例添加到 ggplot2 线图

这给了我以下代码:

library(ggplot2)
ggplot()+
  geom_line(data=myDf, aes(x=count, y=mean, color="TrueMean"))+
  geom_hline(yintercept = myTrueMean, color="SampleMean")+
  scale_colour_manual("",breaks=c("SampleMean", "TrueMean"),values=c("red","blue"))+
  labs(title = "Plot showing convergens of Mean", x="Index", y="Mean")+
  theme_minimal()
Run Code Online (Sandbox Code Playgroud)

如果我删除 的颜色,一切都会正常,但是如果我在不是实际颜色hline的颜色中添加一个值(例如),我会得到一个错误,它不是颜色(仅适用于)。添加一个像传奇这样常见的东西怎么会成为一个大问题呢?还有更简单的方法吗?hline"SampleMean"hline

创建原始数据:

#Initial variables
myAlpha=2
myBeta=2
successes=14
n=20
fails=n-successes

#Posterior values
postAlpha=myAlpha+successes
postBeta=myBeta+fails

#Calculating the mean and SD
myTrueMean=(myAlpha+successes)/(myAlpha+successes+myBeta+fails)
myTrueSD=sqrt(((myAlpha+successes)*(myBeta+fails))/((myAlpha+successes+myBeta+fails)^2*(myAlpha+successes+myBeta+fails+1)))

#Simulate the data
simulateBeta=function(n,tmpAlpha,tmpBeta){
  tmpValues=rbeta(n, tmpAlpha, tmpBeta)
  tmpMean=mean(tmpValues)
  tmpSD=sd(tmpValues)
  returnVector=c(count=n, mean=tmpMean, sd=tmpSD)
  return(returnVector)
}

#Make a df for the data
myDf=data.frame(t(sapply(2:10000, simulateBeta, postAlpha, postBeta)))
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-hline

3
推荐指数
1
解决办法
6812
查看次数

在ggplot2中添加手动图例?

我正在对相同时间序列数据的两个不同估计进行比较图表.如果原始估计值超过最新估计值,我将以绿色填充两个系列之间的区域,否则为红色.

我有那部分工作,但我想为填充颜色添加一个图例.我尝试scale_fill_manual了代码的底部,但它似乎没有做任何事情?

这是代码:

library(ggplot2)
library(scales)
library(colorspace)

# Return a polygon that only plots between yLower and yUpper when yLower is
# less than yUpper.
getLowerPolygon = function(x, yLower, yUpper) {
    # Create the table of coordinates
    poly = data.frame(
        x = numeric(),
        y = numeric())

    lastReversed = (yUpper[1] < yLower[1])
    for (r in 1:length(x)) {
        reversed = (yUpper[r] < yLower[r])
        if (reversed != lastReversed) {
            # Between points r-1 and r, the series intersected, so we need to …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

当每行使用不同的x和y值时,向ggplot2图添加图例

我的问题非常类似于添加图例到ggplot2线图,除了我有多种颜色的情况 - 每个条件1,但它们没有绘制到一个常见的x轴值.相反,我有2个x轴值,使用的值取决于条件.

示例数据:

# dataframe name = df

e0 e200 o0 o50 o200
0.5715707 0.5755010 0.9151736 0.8858229 1.2488826
0.5570928 0.5610231 0.8724417 0.8851889 1.2135041
0.5430821 0.5470124 0.8692482 0.8793603 1.2051914
0.5295093 0.5334396 0.8251555 0.8636917 1.0951763
0.5163479 0.5202782 0.8149114 0.8519220 1.0787246
0.5035736 0.5075039 0.7875460 0.7521003 1.0655470
0.4911643 0.4950946 0.7724218 0.7394516 1.0616154
0.4790998 0.4830301 0.7306038 0.6997307 1.0214771
0.4673614 0.4712917 0.6373668 0.6333903 0.9179331
0.4559320 0.4598622 0.5898641 0.6314342 0.8713423
0.4447956 0.4487259 0.5870693 0.6266098 0.8208793
Run Code Online (Sandbox Code Playgroud)

R脚本:

ggplot(df) +
geom_line(aes(x = e0,y = o0), size …
Run Code Online (Sandbox Code Playgroud)

r linegraph legend ggplot2

-1
推荐指数
1
解决办法
318
查看次数