我正在构建一个条形图,条形图足以作为水平(x)位置的指示,所以我想避免绘制多余的垂直网格线.
我理解如何在opts()中设置次要和主要网格线的样式,但我不能为我的生活弄清楚如何仅仅抑制垂直网格线.
library(ggplot2)
data <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))
ggplot(data, aes(x, y)) +
geom_bar(stat = 'identity') +
opts(
panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
panel.grid.minor = theme_line(colour = NA),
panel.background = theme_rect(colour = NA),
axis.ticks = theme_segment(colour = NA)
)
Run Code Online (Sandbox Code Playgroud)
在这一点上,看起来我将不得不压制所有的网格线,然后用geom_hline()将它们拉回来,这看起来很痛苦(同样,我也不能完全清楚如何找到勾号/主要网格线位置以提供给geom_hline().)
任何想法将不胜感激!
是否有一种简单的方法可以增加绘图标题和它下面的绘图区域之间的空间(带有数据的框).同样,我更喜欢在轴标题和轴标签之间留一些空格.
换句话说,有没有办法"将标题向上移动,y轴标题稍微向左移动,x轴标题稍微向下移动"?
有谁知道如何控制ggplot2中的图例排序?
从我可以看到,订单显示与实际比例标签相关,而不是比例声明顺序.更改比例标题会改变顺序.我用钻石数据集做了一个小例子来强调这一点.我正在尝试将ggplot2用于一系列图表,我想让一个变量出现在右边的所有图表中.目前虽然这只发生在其中一些,但我在如何强制执行我想要的订购同时保留适当的比例标签时不知所措.
library(ggplot2)
diamond.data <- diamonds[sample(nrow(diamonds), 1000), ]
plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +
geom_point() + opts(legend.position = "top", legend.box = "horizontal")
plot # the legend will appear shape then colour
plot + labs(colour = "A", shape = "B") # legend will be colour then shape
plot + labs(colour = "Clarity", shape = "Cut") # legend will be shape then colour
Run Code Online (Sandbox Code Playgroud) 数据:
type size amount
T 50% 48.4
F 50% 48.1
P 50% 46.8
T 100% 25.9
F 100% 26.0
P 100% 24.9
T 150% 21.1
F 150% 21.4
P 150% 20.1
T 200% 20.8
F 200% 21.5
P 200% 16.5
Run Code Online (Sandbox Code Playgroud)
我需要使用ggplot(x轴 - >"type",y轴 - >"amount",group by"size")绘制上述数据的条形图.当我使用下面的代码时,我没有按照数据中显示的顺序获得变量"type"和"size".请看图.我已经使用了以下代码.
ggplot(temp, aes(type, amount , fill=type, group=type, shape=type, facets=size)) +
geom_bar(width=0.5, position = position_dodge(width=0.6)) +
facet_grid(.~size) +
theme_bw() +
scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"),
labels = c("T", "F", "P"))
Run Code Online (Sandbox Code Playgroud)
.
为了解决订单问题,我使用了以下的变量"type"的因子方法.请看图.
temp$new = factor(temp$type, levels=c("T","F","P"), labels=c("T","F","P"))
Run Code Online (Sandbox Code Playgroud)

但是,现在我不知道如何修改变量"size"的顺序.它应该是50%,100%.150%和200%.
我正在用ggplot2绘制线条,如下所示:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw()
Run Code Online (Sandbox Code Playgroud)
.
我发现传奇标记很小,所以我希望它们更大.如果我改变大小,情节上的线条也会改变:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw()
Run Code Online (Sandbox Code Playgroud)
.
但是我只想在图例中看到粗线,我希望图上的线条很薄.我尝试使用,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"))
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用积分:
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + geom_point(size=4) + theme_bw()
Run Code Online (Sandbox Code Playgroud)
但当然它仍会影响情节和传奇:

我想使用线条和图例的点/点.
所以我要问两件事:
我将数据保存为.csv12列的文件.第2至11列(标记为F1, F2, ..., F11)features.Column one包含或label这些功能.goodbad
我想绘制boxplot的所有这些功能11对label,而是通过单独good或bad.到目前为止我的代码是:
qplot(Label, F1, data=testData, geom = "boxplot", fill=Label,
binwidth=0.5, main="Test") + xlab("Label") + ylab("Features")
Run Code Online (Sandbox Code Playgroud)
然而,这只能说明F1反对label.
我的问题是:如何显示F2, F3, ..., F11对label在一个图表一些dodge position?我已将这些特征标准化,因此它们在[0 1]范围内具有相同的比例.
测试数据可以在这里找到.我手工绘制了一些东西来解释这个问题(见下文).

我试图将列索引传递给ggplot作为我将重复使用的函数的一部分.喜欢:
myplot <- function(df){
ggplot(df, aes(df[, 1], df[, 2])) + geom_point()
}
Run Code Online (Sandbox Code Playgroud)
我总是使用第一列作为我的x变量,第二列作为我的y变量,但列名在数据集之间变化.我到处搜索了......有什么想法吗?
这是我用过的答案:
require(ggplot2)
myplot <- function(df){
ggplot(df, aes_string(colnames(df)[1], colnames(df)[2])) + geom_point()
}
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何手动设置此图的图例.我真正想要的只是右边一个简单的图例,它使用了三种颜色并且每个颜色旁边都有一个名称.

当前代码如下所示:
a <-c("S1","S2","S3","S4","S5","S6","S7","S8","S9") #names
b <-c(0.23,0.26,0.55,0.56,0.36,0.23,0.18,0.06,0.04) #mean t0
c <-c(0.64,0.6,0.81,1.4,0.89,0.55,0.48,0.22,0.09) #mean t1
d <-c(0.20,0.23,0.52,0.53,0.33,0.20,0.15,0.04,0.03) #SD low t0
e <-c(0.26,0.29,0.58,.59,0.39,0.26,0.21,0.08,0.05) #SD high t0
f <-c(0.67,0.63,0.86,1.44,0.93,0.59,0.51,0.25,0.10) #SD high t1
g <-c(0.61,0.57,0.78,1.36,0.85,0.53,0.45,0.19,0.08) #SD low t1
h <-c(0.41,0.34,0.26,0.84,0.53,0.32,0.30,0.16,0.05) #absolute change
data <- data.frame(a,b,c,d,e,f,g,h)
ggplot(data=data,aes(a)) +
geom_bar(stat="identity", aes(y=h),fill="#62c76b",colour="#333333")+ #green
geom_line(aes(y=b,group=1),size=1.0,colour="#f04546") + #red
geom_point(aes(y=b),size=3, colour="#f04546") + #red
geom_errorbar(aes(ymin=d, ymax=e), colour="#f04546", width=0.1, size=.8) +
geom_line(aes(y=c,group=1),size=1.0,colour="#3591d1") + #blue
geom_point(aes(y=c),size=3, colour="#3591d1") + #blue
geom_errorbar(aes(ymin=f, ymax=g), colour="#3591d1", width=0.1, size=.8) +
ylab("Symptom severity") + xlab("PHQ-9 symptoms") +
ylim(0,1.6) +
theme_bw() …Run Code Online (Sandbox Code Playgroud) 是否有一种等效的方法来绘制函数使用基本图形中使用ggplot的curve()命令?我想替代方法是创建函数值的向量并绘制连接线,但我希望更简单一些.
谢谢!
我有以下情节:
library(reshape)
library(ggplot2)
library(gridExtra)
require(ggplot2)
data2<-structure(list(IR = structure(c(4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L
), .Label = c("0.13-0.16", "0.17-0.23", "0.24-0.27", "0.28-1"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L), .Label = c("Real queens", "Simulated individuals"
), class = "factor"), value = c(15L, 11L, 29L, 42L, 0L, 5L, 21L,
22L), Legend = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Real queens",
"Simulated individuals"), class = "factor")), .Names = c("IR",
"variable", "value", …Run Code Online (Sandbox Code Playgroud)