R X轴对数刻度转换ggplot2

R X轴对数刻度转换ggplot2,r,ggplot2,R,Ggplot2,我使用RStudio中gapminder包中的数据在ggplot2中创建了一个图表。我很好奇如何将水平轴转换为对数刻度。我确实使用了scale_x_continuous函数,但在运行代码后,图形消失了 ggplot(data = gapminder07) + geom_point(mapping = aes(x = gdpPercap, y = lifeExp)) p <- ggplot(gapminder07, aes(x = gdpPercap, y=lifeExp, label =

我使用RStudio中gapminder包中的数据在ggplot2中创建了一个图表。我很好奇如何将水平轴转换为对数刻度。我确实使用了scale_x_continuous函数,但在运行代码后,图形消失了

ggplot(data = gapminder07) + geom_point(mapping = aes(x = gdpPercap, y = lifeExp))
p <- ggplot(gapminder07, aes(x = gdpPercap, y=lifeExp, label = country))
p + geom_text()
p + scale_x_continuous(trans = 'log10')
ggplot(data=gapminder07)+geom_点(mapping=aes(x=gdpPercap,y=lifeExp))

p您使用第一个
ggplot()
调用创建了一个
ggplot
对象,然后将第二个
ggplot
对象存储到变量
p
中。在下一行中,当您调用
p+geom_text()
时,您将用第二个
ggplot
对象覆盖第一个
ggplot
对象,只需
geom_text()

本质上,您将此代码称为:

ggplot(data = gapminder07) + geom_point(mapping = aes(x = gdpPercap, y = lifeExp))
ggplot(gapminder07, aes(x = gdpPercap, y=lifeExp, label = country)) + geom_text()
ggplot(gapminder07, aes(x = gdpPercap, y=lifeExp, label = country)) + scale_x_continuous(trans = 'log10')
每次调用
p+…
,都会覆盖上一个绘图。相反,你应该做一些像

ggplot(gapminder::gapminder, aes(x = gdpPercap, y = lifeExp, color = country)) + geom_point() + scale_x_continuous(trans = 'log10')
我删除了
geom_text()
调用,因为国家名称刚刚覆盖了整个绘图。我在运行那个代码后得到的

ggplot(gapminder::gapminder, aes(x = gdpPercap, y = lifeExp, color = country)) + geom_point() + scale_x_continuous(trans = 'log10')