ggplotの凡例のギャップを取り除く方法は?
Aug 23 2020
私のデータフレームがあります
Days,Observed,Simulated
0,0,424.8933328
1,1070,1116.781453
2,2360,2278.166227
3,3882,3854.781359
4,5712,5682.090936
5,7508,7565.230044
6,9126,9343.991798
7,10600,10919.17995
8,11893,12249.07067
9,13047,13332.93044
10,14022,14193.53941
11,14852,14863.84784
12,15480,15378.56415
13,16042,15769.6773
14,16362,16064.57556
15,16582,16285.66038
16,16766,16450.70955
17,16854,16573.54275
18,16854,16664.74816
そしてこれは私のコードです、私がいくつかの情報を見逃さなかったことを願っています
dt <- read.csv('data.csv')
days <- dt$Days Observed <- dt$Observed
Simulated <- dt$Simulated
require(ggplot2)
R <- ggplot(dt, aes(x = days))+geom_line(y=Simulated, color="red", size=0.5)+
geom_point(y=Observed, color="midnightblue", size=1.75)
a <- geom_line(aes(y = Simulated, col='Simulated'))
n <- geom_point(aes(y = Observed, fill = "Observed"), col='blue')
c <- ggtitle("2.5kg of Placenta & 0.5kg of seed")
h <- labs(x = 'Time(Days)', y = "Cumulative Biogas Yield(ml)",
colour = NULL, fill = "Legend")
o <- theme(plot.title = element_text(hjust = 0.1))+
theme( plot.title = element_text(colour = "midnightblue"),
axis.title.x = element_text(colour = "black", size = 14),
axis.title.y = element_text(colour = "black", size = 14),
legend.title = element_text(colour = "black", size = 14),
legend.text = element_text(colour = "black", size = 12.5),
axis.text.x = element_text(colour = "black", size = 14),
axis.text.y = element_text(colour = "black", size = 14))
d <- scale_color_manual(values = 'red')
s <- scale_fill_manual(values = 'midnightblue')
Myplot <- R+a+n+c+h+o+d+s
Myplot
私が得た結果は変数間に大きなギャップがあり、削除する必要があります

私が欲しいのは次のとおりです。

画家のグラフを編集して欲しいものを手に入れましたが、面倒な作業で、プロセスを簡単にできるコードが欲しいです。前もって感謝します。
回答
5 chemdork123 Aug 23 2020 at 21:29
2つのテーマ要素の組み合わせを使用して、2つの凡例間の間隔を調整できます:legend.spacing
とlegend.margin
。私はこれらを少しいじってみましたが、この組み合わせはうまくいくようです:
Myplot + theme(
legend.spacing = unit(0,'pt'),
legend.margin = margin(t=0,b=0,unit='pt')
)

NULL
空の文字ではなくに設定されている場合""
。NULL
効果的に削除されます。凡例のタイトルを要素として使用すると、間隔が簡単になりますが、""
何も表されていない場合でも、タイトルの間隔が保持されます。コードでを置き換えるNULL
と""
、これが表示されます...これでうまくいきます:)。2 AllanCameron Aug 23 2020 at 21:29
あなたはただ探していtheme(legend.margin)
ますか?
Myplot + theme(legend.margin = margin(0, 0, -10, 0))
