ggplot2 geom_line图表输出质量差。时间序列线看起来非常模糊

时间:2014-10-19 23:36:52

标签: r ggplot2

我有以下代码段来生成三个时间序列的时间序列折线图“:

ggplot() +
  #theme_light() +
  geom_line(data = Res,aes(x = as.Date(Res$Date), y = Res$AU, color = "AU"), lwd = 0.8) +
  geom_line(data = Res,aes(x = as.Date(Res$Date), y = Res$US, color = "US"), lwd = 0.8) + 
  geom_line(data = Res,aes(x = as.Date(Res$Date), y = Res$UK, color = "UK"), lwd = 0.8) +
  geom_point(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$AU, color = "AU"), size = 5,alpha = 0.5) +
  geom_point(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$US, color = "US"), size = 5,alpha = 0.5) +
  geom_point(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$UK, color = "UK"), size = 5,alpha = 0.5) +
  geom_text(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$AU,  color = "AU", label = currentDp$AU ), vjust = 2, size = 4) + 
  geom_text(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$US,  color = "US", label = currentDp$US ), vjust = 2, size = 4) + 
  geom_text(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$UK,  color = "UK", label = currentDp$UK ), vjust = 2, size = 4) + 
  scale_x_date(breaks = "8 weeks") + 
  ylab(label='') +
  xlab(label='Date') +
  labs(color="") +  
  #because the label is rotated it is the vertical space needs to be adjusted to align the text to the tick marks  
  theme(axis.text.x = element_text(angle = 90,vjust= 0.5)) +
  scale_colour_brewer(palette="Set1")

原始数据框看起来像这样(currentDp数据框只是原始帧的一个片段,包含时间序列的最后一个条目以进行注释):

                  ![Date         AU          US        UK
1  2014-01-02 15:27:00 -0.5829278  0.41575243 0.2067297
2  2014-01-10 19:50:00 -0.6624452  0.45146732 0.1282591
3  2014-01-17 07:58:00 -0.8211819  0.44135680 0.1635130
4  2014-01-24 10:31:00 -0.7604532  0.40848431 0.2925148
5  2014-01-31 09:42:00 -0.7680846  0.19470044 0.2533643
6  2014-02-07 09:10:00 -0.8509427  0.15737371 0.3267309
7  2014-02-14 09:13:00 -0.6053357  0.15124135 0.3884230
8  2014-02-21 09:22:00 -0.7026988  0.10142252 0.3349704
9  2014-02-28 08:47:00 -0.8186539  0.09725343 0.1194897
10 2014-03-07 10:14:00 -0.4283892  0.06974185 0.1529529
11 2014-03-14 08:13:00 -0.4626554  0.09613427 0.1409370
12 2014-03-21 09:47:00 -0.4361898  0.10698534 0.1559909
13 2014-03-28 09:16:00 -0.4361898  0.13999161 0.4359879
14 2014-04-04 09:26:00 -0.5386579  0.16017797 0.4019338
15 2014-04-11 08:52:00 -0.4187301  0.15845762 0.4302058]

然而,输出图像质量看起来很差。我在Windows机器上使用rstudio / R3.1.1 64位。

the output chart

线条看起来有不均匀的厚度,并且在直线上有一些明显的锯齿形。这是正常的吗?我怎样才能提高质量?

我在网上看到的折线图样品都具有非常流畅的质量(即使是显示股价的折线图)

由于

Casbby

更新

我正在使用RStudio菜单中的导出选项。我还没有写出口代码。我将图像导出为jpg / png / bmp。所有人看起来都有同样的问题。另一方面,SVG确实看起来好多了。但我需要使用批处理程序来生成带有嵌入图表的powerpoint,因此我不使用SVG。

2 个答案:

答案 0 :(得分:1)

谢谢hrbrmstr和Marius。我尝试使用以下导出代码的Cairo Package:

library('Cairo')
png(filename="C:\\temp\\epi_test_plain_looking.png",
    type="cairo",
    units="in", 
    width=16, 
    height=8, 
    pointsize=12, 
    res=300)
print(p1)
dev.off()

结果好多了。

Much much better quality

答案 1 :(得分:0)

您可以使用包ReporteRs。它将以矢量图形格式生成您的图形到powerpoint doc。

library( ggplot2 )
library( ReporteRs )

Res = read.table(text="Date\tAU\tUS\tUK
2014-01-02 15:27:00\t-0.5829278\t0.41575243\t0.2067297
2014-01-10 19:50:00\t-0.6624452\t0.45146732\t0.1282591
2014-01-17 07:58:00\t-0.8211819\t0.44135680\t0.1635130
2014-01-24 10:31:00\t-0.7604532\t0.40848431\t0.2925148
2014-01-31 09:42:00\t-0.7680846\t0.19470044\t0.2533643
2014-02-07 09:10:00\t-0.8509427\t0.15737371\t0.3267309
2014-02-14 09:13:00\t-0.6053357\t0.15124135\t0.3884230
2014-02-21 09:22:00\t-0.7026988\t0.10142252\t0.3349704
2014-02-28 08:47:00\t-0.8186539\t0.09725343\t0.1194897
2014-03-07 10:14:00\t-0.4283892\t0.06974185\t0.1529529
2014-03-14 08:13:00\t-0.4626554\t0.09613427\t0.1409370
2014-03-21 09:47:00\t-0.4361898\t0.10698534\t0.1559909
2014-03-28 09:16:00\t-0.4361898\t0.13999161\t0.4359879
2014-04-04 09:26:00\t-0.5386579\t0.16017797\t0.4019338
2014-04-11 08:52:00\t-0.4187301\t0.15845762\t0.4302058", header = TRUE, sep = "\t")
currentDp = tail( Res , n = 1 )

mygraph = ggplot() +
  #theme_light() +
  geom_line(data = Res,aes(x = as.Date(Res$Date), y = Res$AU, color = "AU"), lwd = 0.8) +
  geom_line(data = Res,aes(x = as.Date(Res$Date), y = Res$US, color = "US"), lwd = 0.8) + 
  geom_line(data = Res,aes(x = as.Date(Res$Date), y = Res$UK, color = "UK"), lwd = 0.8) +
  geom_point(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$AU, color = "AU"), size = 5,alpha = 0.5) +
  geom_point(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$US, color = "US"), size = 5,alpha = 0.5) +
  geom_point(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$UK, color = "UK"), size = 5,alpha = 0.5) +
  geom_text(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$AU,  color = "AU", label = currentDp$AU ), vjust = 2, size = 4) + 
  geom_text(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$US,  color = "US", label = currentDp$US ), vjust = 2, size = 4) + 
  geom_text(data = currentDp, aes(x = as.Date(currentDp$Date), y = currentDp$UK,  color = "UK", label = currentDp$UK ), vjust = 2, size = 4) + 
  scale_x_date(breaks = "8 weeks") + 
  ylab(label='') +
  xlab(label='Date') +
  labs(color="") +  
  #because the label is rotated it is the vertical space needs to be adjusted to align the text to the tick marks  
  theme(axis.text.x = element_text(angle = 90,vjust= 0.5)) +
  scale_colour_brewer(palette="Set1")
mygraph

# now produce a pptx and output in a new slide the ggplot object
pptdoc = pptx()
pptdoc = addSlide( pptdoc, "Title and Content")
pptdoc = addPlot( doc = pptdoc, fun = print, x = mygraph )
writeDoc( pptdoc, file = "test.pptx" )