Tag: ggplot2

保存在一个shiny的应用程序进行绘图

我想弄清楚如何使用downloadButton来保存与shiny的情节。 包中的示例演示了downloadButton / downloadHandler以保存.csv。 我将基于此做出一个可重复的例子。 对于ui.R shinyUI(pageWithSidebar( headerPanel('Downloading Data'), sidebarPanel( selectInput("dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars")), downloadButton('downloadData', 'Download Data'), downloadButton('downloadPlot', 'Download Plot') ), mainPanel( plotOutput('plot') ) )) 对于server.R library(ggplot2) shinyServer(function(input, output) { datasetInput <- reactive({ switch(input$dataset, "rock" = rock, "pressure" = pressure, "cars" = cars) }) plotInput <- reactive({ df <- datasetInput() […]

如何在ggplot中更改行宽?

数据链接:使用的数据 我的代码: ccfsisims <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_ConsIndex.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE) ccfsirsts <- as.data.frame(ccfsisims) ccfsirsts[6:24] <- sapply(ccfsirsts[6:24],as.numeric) ccfsirsts <- droplevels(ccfsirsts) ccfsirsts <- transform(ccfsirsts,sres=factor(sres,levels=unique(sres))) library(ggplot2) #—————————————————————————————— #### Plot of food security index for Morocco and Turkey by sector #—————————————————————————————— #_Code_Begin… datamortur <- melt(ccfsirsts[ccfsirsts$region %in% c("TUR","MAR"), ]) # Selecting regions of interest datamortur1 <- datamortur[datamortur$variable […]

R + ggplot:带有事件的时间序列

我是R / ggplot新手。 我想创build一个连续variables时间序列的geom_line图,然后添加一个由事件组成的图层。 连续variables及其时间戳存储在一个data.frame中,事件和时间戳存储在另一个data.frame中。 我真正想要做的就是像finance.google.com上的图表。 其中,时间序列是股票价格,有“旗”来表示新闻事件。 我实际上不是在绘制财务资料,但是图表的types是相似的。 我试图绘制日志文件数据的可视化。 这里是我的意思的例子… 如果build议(?),我想为每个图层使用不同的数据框架(一个用于连续variables观察,另一个用于事件)。 经过一些试验和错误,这是尽可能接近我可以得到的。 在这里,我正在使用来自ggplot的数据集的示例数据。 “经济学”包含一些我想绘制的时间序列数据,“总统”包含一些事件(总统选举)。 library(ggplot2) data(presidential) data(economics) presidential <- presidential[-(1:3),] yrng <- range(economics$unemploy) ymin <- yrng[1] ymax <- yrng[1] + 0.1*(yrng[2]-yrng[1]) p2 <- ggplot() p2 <- p2 + geom_line(mapping=aes(x=date, y=unemploy), data=economics , size=3, alpha=0.5) p2 <- p2 + scale_x_date("time") + scale_y_continuous(name="unemployed [1000's]") p2 <- p2 […]

ggplot2图例底部和水平

如何将ggplot2图例移动到图的底部并水平翻转? 示例代码: library(reshape2) # for melt df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2")) p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value)) p1 + scale_fill_continuous(guide = guide_legend()) 期望的(近似的)结果:

删除图例ggplot 2.2

我试图保留一层的传说(光滑),并用show.legend参数去除另一个(点)的传说,但传说棒。 这里是代码: ggplot(a[a$variable=="ratio1",], aes(x=new.cor1, y=value))+ geom_point(aes(x=new.cor1, y=value, color=mntn1…1., show.legend=F), size=0.001)+ geom_point(data=tbl1.cnds1, aes(x=new.cor1.cnds, y=ratio1.cnds), shape=5)+ geom_smooth(data=a, aes(x=new.cor1, y=value, colour=variable, show.legend=T), size=0.5, span=0.05, show.legend=T)+ labs(title="Manhattan plot", x="position", y="zygosity score", colour = "", fill="")+ theme(axis.text.x = element_text(size=11), axis.text.y = element_text(size=18), legend.text=element_text(size=17), legend.key.size = unit(1, "cm"), plot.title=element_text(size=25, vjust=3), plot.margin = unit(c(1,0.9,1,1), "cm"), axis.text=element_text(size=10), axis.title = element_text(size=24), axis.title.y=element_text(margin=margin(r = 13)), […]

有没有办法改变ggplot2中的图例项目之间的间距?

有没有办法改变ggplot2中的图例项目之间的间距? 我现在有 legend.position ="top" 自动产生一个水平的图例。 然而,物品的间距非常接近,我想知道如何将它们分开。

控制ggplot2图例而不影响绘图

我正在用ggplot2来绘制线条,如下所示: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw() 。 我发现传说中的痕迹很小,所以我希望它们更大。 如果我改变尺寸,情节上的线条也会改变: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw() 。 但是我只想在传说中看到粗线条,我想让情节上的线条变细。 我试图使用legend.key.size但它改变了标记的平方,而不是线的宽度: library(grid) # for unit ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw() + theme(legend.key.size=unit(1,"cm")) 我也试着用点: ggplot(iris,aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + geom_point(size=4) + theme_bw() 当然,它仍然影响剧情和传奇: 我想用图线和图例中的点/点。 所以我问了两件事情: 如何改变图例中的线条宽度而不改变graphics? 如何绘制图中的线条,但在图例中绘制点/点/方块?

R:ggplot不工作,如果它在一个for循环,虽然它的作品以外它

我正在使用一个简单的ggplot函数,它可以在循环外部正常工作,但即使迭代值不会干扰ggplot函数,也不会在内部工作。 为什么这样? 这是我的代码 x=1:7 y=1:7 df = data.frame(x=x,y=y) ggplot(df,aes(x,y))+geom_point() 有用 ! 但是如果ggplot在for循环中… for (i in 1:5) { ggplot(df,aes(x,y))+geom_point() } …它不工作了! 我错过了什么? 谢谢

小平面标签字体大小

有没有办法改变ggplot的facet标签的字体大小? 我谷歌了一下,发现这个问题还没有在哈德利的名单上。 我想知道在这个问题上是否有解决方法或任何消息。 Thx分享任何消息

删除ggplot中的图例标题

我试图删除ggplot2中的图例标题: df <- data.frame( g = rep(letters[1:2], 5), x = rnorm(10), y = rnorm(10) ) library(ggplot2) ggplot(df, aes(x, y, colour=g)) + geom_line(stat="identity") + theme(legend.position="bottom") 我已经看到这个问题 ,没有任何解决scheme似乎为我工作。 大多数给出了一个关于opts如何被弃用和使用theme的错误。 我也尝试过各种版本的theme(legend.title=NULL) , theme(legend.title="") , theme(legend.title=element_blank)等等。典型的错误信息是: 'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1) 'theme_blank' is deprecated. Use 'element_blank' instead. (Deprecated; last used in version […]