Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何仅标记ggplot2中的某些点_R_Ggplot2_Data Visualization - Fatal编程技术网

R 如何仅标记ggplot2中的某些点

R 如何仅标记ggplot2中的某些点,r,ggplot2,data-visualization,R,Ggplot2,Data Visualization,我有一个数据框架global,有三列Year,MTCO2,和Scenario。我已经创建了一个散点图,看起来像我想要的那样,但我正在努力解决如何最好地标记图上的几个点 这是我目前的代码: plot1 <- global %>% ggplot(aes(Year, MTCO2, group = Scenario, colour = Scenario)) + geom_line(size = 1) + geom_point(size = 2) + scale_x_conti

我有一个数据框架
global
,有三列
Year
MTCO2
,和
Scenario
。我已经创建了一个散点图,看起来像我想要的那样,但我正在努力解决如何最好地标记图上的几个点

这是我目前的代码:

plot1 <- global %>% 
ggplot(aes(Year, MTCO2, group = Scenario, colour = Scenario)) + 
  geom_line(size = 1) +
  geom_point(size = 2) +
  scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
  theme_bw() +
  labs(title = "Global CO2 Emissions Projections with and without Constraints")
plot1a <- plot1 + geom_text(data = subset(global, Year == 2006 | Year == 2030 | Year == 2050 ),
                                     aes(Year, MTCO2, label = MTCO2))

我想有几种方法可以解决这个问题。在对您的代码进行最小更改的情况下,我尝试了以下方法:

library(ggplot2)
plot1 <- global %>% 
  ggplot(aes(Year, MTCO2, group = Scenario, colour = Scenario)) + 
  geom_line(size = 1) +
  geom_point(size = 2) +
  scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
  theme_bw() +
  labs(title = "Global CO2 Emissions Projections with and without Constraints")
plot1a <- plot1 + geom_text(data = subset(global, Year == 2006 | Year == 2030 | Year == 2050 ),
                            aes(Year, MTCO2, label = MTCO2), 
                            color = "black", nudge_x = -6)
库(ggplot2)
1%
ggplot(aes(年份、MTCO2、组=情景、颜色=情景))+
几何尺寸线(尺寸=1)+
几何点(尺寸=2)+
比例x连续(name=“Year”,breaks=c(1990、2000、2006、2020、2030、2040、2050))+
主题_bw()+
实验室(title=“有约束和无约束的全球二氧化碳排放预测”)

plot1a我想有几种方法可以解决这个问题。在对您的代码进行最小更改的情况下,我尝试了以下方法:

library(ggplot2)
plot1 <- global %>% 
  ggplot(aes(Year, MTCO2, group = Scenario, colour = Scenario)) + 
  geom_line(size = 1) +
  geom_point(size = 2) +
  scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
  theme_bw() +
  labs(title = "Global CO2 Emissions Projections with and without Constraints")
plot1a <- plot1 + geom_text(data = subset(global, Year == 2006 | Year == 2030 | Year == 2050 ),
                            aes(Year, MTCO2, label = MTCO2), 
                            color = "black", nudge_x = -6)
库(ggplot2)
1%
ggplot(aes(年份、MTCO2、组=情景、颜色=情景))+
几何尺寸线(尺寸=1)+
几何点(尺寸=2)+
比例x连续(name=“Year”,breaks=c(1990、2000、2006、2020、2030、2040、2050))+
主题_bw()+
实验室(title=“有约束和无约束的全球二氧化碳排放预测”)

绘图1a我认为最简单的方法是创建标签,然后设计绘图:

#Format data
global %>% mutate(label=ifelse(Year %in% c(2006,2030,2050),MTCO2,NA)) -> global
#Plot
ggplot(global,aes(x=Year, y=MTCO2,label=label)) + 
  geom_line(size = 1,aes(color = Scenario)) +
  geom_point(size = 2,aes(color = Scenario)) +
  geom_text(vjust=-1)+
  scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
  theme_bw() +
  labs(title = "Global CO2 Emissions Projections with and without Constraints")

我认为最简单的方法是创建标签,然后设计绘图:

#Format data
global %>% mutate(label=ifelse(Year %in% c(2006,2030,2050),MTCO2,NA)) -> global
#Plot
ggplot(global,aes(x=Year, y=MTCO2,label=label)) + 
  geom_line(size = 1,aes(color = Scenario)) +
  geom_point(size = 2,aes(color = Scenario)) +
  geom_text(vjust=-1)+
  scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
  theme_bw() +
  labs(title = "Global CO2 Emissions Projections with and without Constraints")

您可以使用
annotate()
将标签放在绘图上任何需要的地方,使用
guides()
抑制或覆盖不需要的图例。您可以使用
annotate()
将标签放在绘图上任何需要的地方,使用
guides()
抑制或覆盖不需要的图例。