Tag: ggplot2

ggplot折线图中的多行x轴标签

编辑:这个问题已被标记为重复,但在这里的答复已经尝试,并没有工作,因为有问题的情况是一个折线图,而不是条形图。 应用这些方法会产生一个5行的图表,每年1个 – 没有用处。 有人投票标记为重复实际上尝试这个问题提供的样本数据集的方法? 如果是这样,请发帖作为答案。 原问题: Excel透视图中有一个允许多级分类坐标轴的function。我试图find一种方法来使用ggplot (或R中的任何其他绘图软件包)执行相同的操作。 考虑以下数据集: set.seed(1) df=data.frame(year=rep(2009:2013,each=4), quarter=rep(c("Q1","Q2","Q3","Q4"),5), sales=40:59+rnorm(20,sd=5)) 如果将其导入到Excel数据透视表中,则可以直接创build以下图表: 请注意x轴有两个级别,分别为一个季度和一个分组variables,一年。 ggplot可以使用多级轴吗? 注意:有一个方面可以产生类似的黑客攻击,但这不是我正在寻找的。 library(ggplot2) ggplot(df) + geom_line(aes(x=quarter,y=sales,group=year))+ facet_grid(.~year,scales="free")

R堆积百分比条形图与二进制因子和标签的百分比(与ggplot)

我想生成一个看起来像这样的graphics: 我的原始数据集看起来像这样: > bb[sample(nrow(bb), 20), ] IMG QUANT FIX 25663 1 1 0 7936 2 2 0 23586 3 2 0 23017 2 2 1 31363 1 3 1 7886 2 2 0 23819 3 3 1 29838 2 2 1 8169 2 3 1 9870 2 3 0 31440 2 1 0 35564 3 […]

创build堆叠的barplot,其中每个堆栈被缩放到总和为100%

我有这样的data.frame: df <- read.csv(text = "ONE,TWO,THREE 23,234,324 34,534,12 56,324,124 34,234,124 123,534,654") 我想产生一个百分比条形图,看起来像这样(用LibreOffice Calc制作): 因此,酒吧应该标准化,所有的堆栈有相同的高度和总和为100%。 到目前为止,我所能得到的是一个堆叠的barplot(不是百分比),使用: barplot(as.matrix(df)) 任何帮助?

ggplot2:sorting一个情节

我有一个data.frame,从最高到最低sorting。 例如: x <- structure(list(variable = structure(c(10L, 6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), class = c("ordered", "factor")), value = c(0.990683229813665, 0.975155279503106, 0.928571428571429, 0.807453416149068, 0.717391304347826, 0.388198757763975, 0.357142857142857, 0.201863354037267, 0.173913043478261, 0.0496894409937888)), .Names = c("variable", "value"), row.names = c(10L, 6L, 3L, 4L, 2L, […]

将geom_text定位在闪避的barplot上

我试图让这个标题不言自明,但是数据优先: dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"), ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola", "fakultet"), class = "factor"), `(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139, 33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692, 23.9426605504587, […]

在ggplot2的各个方面注释文本

我想用以下代码在剧情的最后一个方面注释一些文字: library(ggplot2) p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p <- p + facet_grid(. ~ cyl) p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5) print(p) 但是这个代码注释了每个方面的文本。 如果您指导我如何仅在一个方面获得注释文本,我将非常感激。 提前致谢。

我怎么能把一个转换的规模在ggplot2的右侧?

我正在创build一个图表,显示随着时间的推移湖面水平的变化。 我附上了一个简单的例子。 我想添加一个比例尺(刻度线和注释)在图表的右侧,显示以英尺为单位的高程。 我知道ggplot2不会允许两个不同的尺度(见2 y轴,一个y轴在左边,另一个y轴在右边 ),但是因为这是一个相同比例的变换,有没有办法做这个? 我宁愿继续使用ggplot2,而不必恢复到plot()函数。 library(ggplot2) LakeLevels<-data.frame(Day=c(1:365),Elevation=sin(seq(0,2*pi,2*pi/364))*10+100) p <- ggplot(data=LakeLevels) + geom_line(aes(x=Day,y=Elevation)) + scale_y_continuous(name="Elevation (m)",limits=c(75,125)) p

添加一个共同的图例组合ggplots

我有两个ggplots,我水平alignmentgrid.arrange 。 我已经浏览了很多论坛post,但是我尝试的所有东西似乎都是现在已更新并命名为其他内容的命令。 我的数据看起来像这样; # Data plot 1 axis1 axis2 group1 -0.212201 0.358867 group2 -0.279756 -0.126194 group3 0.186860 -0.203273 group4 0.417117 -0.002592 group1 -0.212201 0.358867 group2 -0.279756 -0.126194 group3 0.186860 -0.203273 group4 0.186860 -0.203273 # Data plot 2 axis1 axis2 group1 0.211826 -0.306214 group2 -0.072626 0.104988 group3 -0.072626 0.104988 group4 -0.072626 0.104988 group1 0.211826 -0.306214 […]

在ggplot2中有边缘直方图的散点图

有没有办法用边缘直方图创build散点图,就像在ggplot2中的样例ggplot2 ? 在Matlab中,它是scatterhist()函数,也存在R的等价物。 但是,我没有看到它的ggplot2。 我开始尝试创build单个graphics,但不知道如何正确安排它们。 require(ggplot2) x<-rnorm(300) y<-rt(300,df=2) xy<-data.frame(x,y) xhist <- qplot(x, geom="histogram") + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 5/16, axis.text.y = theme_blank(), axis.title.y=theme_blank(), background.colour="white") yhist <- qplot(y, geom="histogram") + coord_flip() + opts(background.fill = "white", background.color ="black") yhist <- yhist + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = […]

与ggplot2并排绘图

我想使用ggplot2软件包并排放置两个图,即做相当于par(mfrow=c(1,2)) 。 例如,我希望以下两个地块以相同的比例并排显示。 x <- rnorm(100) eps <- rnorm(100,0,.2) qplot(x,3*x+eps) qplot(x,2*x+eps) 我需要把它们放在相同的data.frame? qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()