如何更改geom_text图例符号?在下面的示例中,我想将图例键中的符号从小写"a"更改为,例如,大写"N".我已经看过一个在这里做类似事情的例子,但是无法让这个例子起作用.
# Some toy data
df <- expand.grid(x = factor(seq(1:5)), y = factor(seq(1:5)), KEEP.OUT.ATTRS = FALSE)
df$Count = seq(1:25)
# An example plot
library(ggplot2)
ggplot(data = df, aes( x = x, y = y, label = Count, size = Count)) +
geom_text() +
scale_size(range = c(2, 10))
Run Code Online (Sandbox Code Playgroud)

我的数据框是
df = read.table(text = "
id year type amount
1 1991 HIIT 22
2 1991 inter 144
3 1991 VIIT 98
4 1992 HIIT 20
5 1992 inter 136
6 1992 VIIT 108
7 1993 HIIT 20
8 1993 inter 120
9 1993 VIIT 124
10 1994 HIIT 26
11 1994 inter 118
12 1994 VIIT 120
13 1995 HIIT 23
14 1995 inter 101
15 1995 VIIT 140
16 1996 HIIT 27
17 1996 inter 103
18 1996 …Run Code Online (Sandbox Code Playgroud) 我正在努力增加图例标题和标签之间的空间,ggplot2但没有运气应用我可以在网上找到的各种可能的解决方案.正如您在下面的可重现示例中所看到的,标题文本太靠近顶部数字.我想避免手动添加线制动器(\n)的笨重的解决方案(下面的#6),因为这不允许调整间距尺寸,在我的情况下完整的线太多了.
我需要用一个colourbar.我知道vjust通常需要0到1之间的值,但我使用下面的值2来更容易地检测更改.
library(reshape2)
library(ggplot2)
# Generate test data frame
df=reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
# Declare theme
mytheme=theme_classic(base_size=15) +
theme(axis.title.x=element_blank(),axis.title.y=element_blank(),
axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks=element_blank()) +
theme(legend.position=c(0,1), legend.justification=c(0,1),
legend.title=element_text(size="12",face = "bold"))
# Plot
p=ggplot(data=df, aes_string(x="X1", y="X2")) +
geom_tile(aes(fill=value))+
scale_fill_gradient(low="yellow",high="red",guide="colourbar",name="Titleggplot") +
annotate("text",x=Inf,y=Inf,label="(a)" ,hjust=1.5, vjust=1.5, size=6) +
mytheme
p
#*** Things I tried (building on the defaults above) that do not work
# 1 - set "vjust" in theme
mytheme=mytheme+theme(legend.title=element_text(size="12",face = "bold",vjust=2))
p=p+mytheme
p
# Result: …Run Code Online (Sandbox Code Playgroud) 我非常喜欢directlabels动态定位情节标签的包.虽然仍然不完美,但它有助于合理地防止散点图的标签与多点之间的重叠 - 特别是对于图表的草稿版本而不是最终版本.
但是在使用geom_dl()功能时无论如何我都找不到更改标签尺寸.反正呢?
我试图传递size和cex作为aes()函数的正常或参数.
在这两个图中,这些点看起来不同,但为什么呢?
mya <- data.frame(a=1:100)
ggplot() +
geom_path(data=mya, aes(x=a, y=a, colour=2, size=seq(0.1,10,0.1))) +
geom_point(data=mya, aes(x=a, y=a, colour=1, size=1)) +
theme_bw() +
theme(text=element_text(size=11))
ggplot() +
geom_path(data=mya, aes(x=a, y=a, colour=2, size=1)) +
geom_point(data=mya, aes(x=a, y=a, colour=1, size=1)) +
theme_bw() +
theme(text=element_text(size=11))
Run Code Online (Sandbox Code Playgroud)
?aes_linetype_size_shape 解释......
# Size examples
# Should be specified with a numerical value (in millimetres),
# or from a variable source
Run Code Online (Sandbox Code Playgroud)
但在我的代码中它看起来不同.
很多时候在C程序的例子中,我遇到过这种循环.这些循环真正做了什么?
do {
while (...) // Check some condition if it is true.
{
calculation 1
}
// Some new condition is checked.
} while(true);
Run Code Online (Sandbox Code Playgroud)
有什么需要while(true);用于无限循环?有人可以解释上面的循环真正做了什么.我是C编程的新手
我不是试图解决任何特定问题,而是试图学习R并理解其逻辑否定运算符"!" 记录在页面http://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html
当与=结合使用时,它适用于我,例如:
1 != 2
TRUE
Run Code Online (Sandbox Code Playgroud)
但我无法理解这个运营商的独立应用.例如,我可以使用它来选择列表中没有特定名称的元素.这是我尝试这样做的,但它不起作用:
vector1 <- 1:5 # just making vector of 5 numbers
vector2 <- 5:1 # same vector backwards
list <- list(Forward=vector1, Backwards=vector2) # producing list with two elements
x = "Forward"
list[!x]
Run Code Online (Sandbox Code Playgroud)
我的输出是:
Error in !x : invalid argument type
Run Code Online (Sandbox Code Playgroud)
我会理解在这种情况下我的逻辑出错的任何提示,以及除了!= case之外该运算符的其他好用途.
谢谢!谢尔盖
在下面的图中,直接标签位置有点垂直调整,但它们在左/右边缘被剪裁.有没有办法避免剪裁(类似xpd=TRUE)或在绘图框中向内调整剪切的标签?

这是这个例子的代码:
library(car)
library(reshape2)
library(ggplot2)
library(directlabels)
library(nnet)
## Sec. 8.2 (Nested Dichotomies)
# transform data
Womenlf <- within(Womenlf,{
working <- recode(partic, " 'not.work' = 'no'; else = 'yes' ")
fulltime <- recode(partic,
" 'fulltime' = 'yes'; 'parttime' = 'no'; 'not.work' = NA")})
mod.working <- glm(working ~ hincome + children, family = binomial,
data = Womenlf)
mod.fulltime <- glm(fulltime ~ hincome + children, family = binomial,
data = Womenlf)
predictors <- expand.grid(hincome = 1:50,
children = c("absent", "present"))
fit …Run Code Online (Sandbox Code Playgroud) 我正在尝试为2只股票AAPL和FB创建一个线图.我想打印股票代码和线条,而不是添加单独的图例.如何将geom_text添加到以下代码中?我感谢您提供的任何帮助.
library (ggplot2)
library(quantmod)
getSymbols('AAPL')
getSymbols('FB')
AAPL = data.frame(AAPL)
FB = data.frame(FB)
p1 = ggplot(AAPL)+geom_line(data=AAPL,aes(as.Date(rownames(AAPL)),AAPL.Adjusted,color="AAPL"))
p2 = p1+geom_line(data=FB,aes(as.Date(rownames(FB)),FB.Adjusted,color="FB"))
p2 + xlab("Year")+ylab("Price")+theme_bw()+theme(legend.position="none")
Run Code Online (Sandbox Code Playgroud) 我编写了一个函数来使用ggplot函数获得比例堆积条形图.现在我在这里使用列名ID.
PropBarPlot<-function(df, mytitle=""){
melteddf<-melt(df, id="ID", na.rm=T)
ggplot(melteddf, aes(ID, value, fill=variable)) +
geom_bar(position="fill") +
theme(axis.text.x = element_text(angle=90, vjust=1)) +
labs(title=mytitle)
}
Run Code Online (Sandbox Code Playgroud)
我想让它变得通用.所以我想使用列索引而不是列名.我尝试过做这样的事情.
PropBarPlot<-function(df, mytitle=""){
melteddf<-melt(df, id=names(df)[1], na.rm=T)
ggplot(melteddf, aes(names(df)[1], value, fill=variable)) +
geom_bar(position="fill") +
theme(axis.text.x = element_text(angle=90, vjust=1)) +
labs(title=mytitle)
}
Run Code Online (Sandbox Code Playgroud)
但没有用.谁能建议我怎么做?
谢谢.