Tag: ggplot2

如何更改使用ggplot2制作的图的背景颜色

默认情况下,ggplot2生成灰色背景的图。 如何更改剧情背景的颜色? 例如,一个由以下代码产生的情节: library(ggplot2) myplot<-ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + geom_line() myplot

用R中的ggplot2覆盖直方图

我是R新手,想把3个直方图绘制到同一个图上。 一切正常,但我的问题是,你没有看到2直方图重叠 – 他们看起来相当切断: 直方图 当我制作密度图时,它看起来很完美:每条曲线都被一条黑色边框线包围,曲线重叠处的颜色看起来不同: 密度图 有人能告诉我,如果第一张照片中的直方图可以达到类似的效果吗? 这是我正在使用的代码: lowf0 <-read.csv (….) mediumf0 <-read.csv (….) highf0 <-read.csv(….) lowf0$utt<-'low f0' mediumf0$utt<-'medium f0' highf0$utt<-'high f0' histogram<-rbind(lowf0,mediumf0,highf0) ggplot(histogram, aes(f0, fill = utt)) + geom_histogram(alpha = 0.2) 预先感谢任何有用的提示!

如何处理来自ggplot2的“不同类的数据”错误?

当试图覆盖一个新的行到现有的ggplot我收到以下错误: Error: ggplot2 doesn't know how to deal with data of class uneval 我的代码的第一部分工作正常。 以下是来自美国中西部电力市场的“近期”每小时风力发电数据的图像。 现在我想把最后两天的观察结果叠加在红色上。 这应该很容易,但我不知道为什么我得到一个错误。 任何援助将不胜感激。 以下是一个可重现的例子: # Read in Wind data fname <- "https://www.midwestiso.org/Library/Repository/Market%20Reports/20130510_hwd_HIST.csv" df <- read.csv(fname, header=TRUE, sep="," , skip=7) df <- df[1:(length(df$MKTHOUR)-5),] # format variables df$MWh <- as.numeric(df$MWh) df$Datetime <- strptime(df$MKTHOUR, "%m/%d/%y %I:%M %p") # Create some variables df$Date <- […]

如何使用ggplot2在R中使用透明背景制作graphics?

我需要输出ggplot2graphics从R到透明背景的PNG文件。 基本的Rgraphics一切都好,但ggplot2没有透明度: d <- rnorm(100) #generating random data #this returns transparent png png('tr_tst1.png',width=300,height=300,units="px",bg = "transparent") boxplot(d) dev.off() df <- data.frame(y=d,x=1) p <- ggplot(df) + stat_boxplot(aes(x = x,y=y)) p <- p + opts( panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank() panel.grid.minor = theme_blank(), panel.grid.major = theme_blank() ) #returns white background png('tr_tst2.png',width=300,height=300,units="px",bg = "transparent") […]

如何获得一个垂直的geom_vline到类date的x轴?

即使我发现在POSIXct和geom_vline的谷歌组中的哈德利的post,我无法完成。 例如,我有一个时间序列,想要绘制1998年,2005年和2010年的垂直线。 我尝试了ggplot和qplot语法,但是我仍然没有看到垂直线,或者垂直线是在第一个垂直网格上绘制的,整个系列有点奇怪地向右移动。 gg <- ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) + layer(geom="line") gg + geom_vline(xintercept=mydata$datefield[120],linetype=4) # returns just the time series plot I had before, # interestingly the legend contains dotted vertical lines 我的date字段格式为“1993-07-01”,类别为Date 。

如何更改ggplot2中的默认字体大小

我想知道是否有可能改变ggplot2graphics的一些默认参数,例如整个R会话的字体大小。 这个想法是为了避免为每个情节设置它们。

ggplot2折线图给出了“geom_path:每个组只包含一个观察值。 你需要调整团体审美吗?“

有了这个dataframe(“DF”): year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008 88.27546 我尝试创build一个这样的折线图: plot5 <- ggplot(df, aes(year, pollution)) + geom_point() + geom_line() + labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore") 我得到的错误是: geom_path:每个组只包含一个观察值。 你需要调整团体审美吗? 即使我想要一个折线图,该图表也会显示为散点图。 我尝试用geom_line(aes(group = year))replacegeom_line() ,但是没有成功。 在一个答案中,我被告知将年份转换为因子variables。 我做了,问题依然存在。 这是str(df)和dput(df) : 'data.frame': […]

更改ggplot2中的轴标题和标签的大小

我有一个非常简单的问题,我正在努力寻找答案。 我希望这里有人能够帮助我。 下面介绍一个示例数据框: a <- c(1:10) b <- c(10:1) df <- data.frame(a,b) library(ggplot2) g = ggplot(data=df) + geom_point(aes(x=a, y=b)) + xlab("x axis") g 我只想了解如何更改轴标题和轴标签的文本大小。

忽略ggplot2 boxplot中的exception值

我将如何忽略ggplot2 boxplot中的exception值? 我不只是希望他们消失(即outlier.size = 0),但我希望他们被忽略,这样的y轴缩放显示第1 /第3百分位。 我的exception值正在导致“盒子”缩小到几乎是一条线。 有没有一些技术来解决这个问题? 编辑这是一个例子: y = c(.01, .02, .03, .04, .05, .06, .07, .08, .09, .5, -.6) qplot(1, y, geom="boxplot")

错误:找不到function“单元”

在尝试修改主题设置时,这个简单的代码给出了以下错误: library(ggplot2) theme_nogrid <- theme_set(theme_update( plot.margin=unit(c(.25, .25, .25, .25), "in"),)) Error in do.call(theme, list(…)) : could not find function "unit" R给我这个错误的任何元素,使用“单位”。 任何其他设置不会调用“单位”工作正常。 我正在运行R v.2.15.2(64位Windows)。 我在网上广泛search了这个问题,什么都没发现。 我很欣赏这个问题的任何build议。