我想使用ggplot2
的{{1}}创建一个图形,如下所示:
facet_grid
这很棒,但是因为要写在期刊文章中,所以每个面板也都需要用a,b,c等标记。软件包# Load ggplot2 library for plotting
library(ggplot2)
# Plot dummy data
p <- ggplot(mtcars, aes(mpg, wt))
p <- p + geom_point()
p <- p + facet_grid(gear ~ cyl)
print(p)
的功能很强大,称为egg
,其用法如下:
tag_facet
由reprex package(v0.2.1)于2019-05-09创建
根据需要,我现在如何在每个面板上标记字母。但是,正如您所看到的,我的带标签已经消失了!
我的问题:如何保留带状标签,同时还要添加标签?
答案 0 :(得分:2)
您可以查看tag_facet
here的代码。如您所见,该函数显式并有意删除了切面条(另请参见documentation中的“值”)。您可以通过创建自己的函数并从原始代码中删除theme
调用来解决此问题:
tag_facet2 <- function(p, open = "(", close = ")", tag_pool = letters, x = -Inf, y = Inf,
hjust = -0.5, vjust = 1.5, fontface = 2, family = "", ...) {
gb <- ggplot_build(p)
lay <- gb$layout$layout
tags <- cbind(lay, label = paste0(open, tag_pool[lay$PANEL], close), x = x, y = y)
p + geom_text(data = tags, aes_string(x = "x", y = "y", label = "label"), ..., hjust = hjust,
vjust = vjust, fontface = fontface, family = family, inherit.aes = FALSE)
}
答案 1 :(得分:2)
使用{tagger}软件包,可以使它变得更简单。您可以使用tagger
安装devtools::install_github("eliocamp/tagger")
。安装标记器后,让我们加载它。
library(tagger)
library(ggplot2)
# Plot dummy data
p <- ggplot(mtcars, aes(mpg, wt))
p <- p + geom_point()
p + facet_grid(gear ~ cyl) + tag_facets()
答案 2 :(得分:1)
只能使用geom_text()
来完成此操作:
data_text <- data.frame(
cyl = c(4, 6, 8, 4, 6, 8, 4, 6, 8),
gear = c(3, 3, 3, 4, 4, 4, 5, 5, 5)
label = c('(a)', '(b)', '(c)', '(d)', '(e)', '(f)', '(g)', '(h)', '(i)')
)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_grid(gear ~ cyl) +
geom_text(data=data_text, aes(x=12, y=5, label=label), fontface='bold', size=4)
答案 3 :(得分:0)
当然,在询问后我立即找到了解决方案。问题似乎是import {Service} from 'typedi';
import {helper} from '../../Utils/Service/OpenWeatherMapHelper';
import {Connection, getConnectionManager, getCustomRepository, Repository} from "typeorm";
import {Weather} from "../entity/Weather";
import {WeatherLocalizationMethod} from "../entity/WeatherLocalizationMethod";
import {WeatherLocation} from "../entity/WeatherLocation";
import {LocalizationMethodType} from "../type/LocalizationMethodType";
import {WeatherRepository} from "../repository/WeatherRepository";
import {ILocalizationMethod} from "../message/ILocalizationMethod";
import {ILocation} from "../message/ILocation";
@Service()
export class WeatherService {
connectionManager: Connection;
weatherRepository: WeatherRepository;
localizationMethodRepository: Repository<WeatherLocalizationMethod>;
locationRepository: Repository<WeatherLocation>;
constructor(
) {
this.connectionManager = getConnectionManager().get();
this.weatherRepository = getCustomRepository(WeatherRepository);
this.localizationMethodRepository = this.connectionManager.manager.getRepository(WeatherLocalizationMethod);
this.locationRepository = this.connectionManager.manager.getRepository(WeatherLocation);
}
async getCurrentAPIWeather() {
const locationMethod = await this.localizationMethodRepository
.createQueryBuilder('lm')
.where("active = :active", {active: true})
.getOne();
const locationQb = await this.locationRepository.createQueryBuilder('l');
const location: WeatherLocation = await locationQb
.where("created_at = " + locationQb.subQuery().select('MAX(sl.created_at)').from(WeatherLocation, 'sl').getQuery())
.getOne();
switch (locationMethod.name) {
case LocalizationMethodType.COORDS:
helper.getCurrentWeatherByGeoCoordinates(location.longitude, location.latitude, (err, currentWeather) => {
const weather = this.mapWeatherEntity(currentWeather);
this.connectionManager.manager.save(weather);
});
break;
case LocalizationMethodType.NAME:
helper.getCurrentWeatherByCityName(location.locationName, (err, currentWeather) => {
const weather = this.mapWeatherEntity(currentWeather);
this.connectionManager.manager.save(weather);
});
break;
}
}
public getLastWeather() {
return this.weatherRepository.findCurrentWeather();
}
public async toggleLocalizationMethod(method: ILocalizationMethod) {
const foundMethod = await this.localizationMethodRepository.findOne({name: method.name});
if (!foundMethod) {
throw new Error("Method with name: " + method.name + " not found");
}
foundMethod.active = true;
this.localizationMethodRepository.save(foundMethod);
}
public async updateLocation(location: ILocation) {
const weatherLocation = new WeatherLocation();
weatherLocation.locationName = location.name;
weatherLocation.longitude = location.long;
weatherLocation.latitude = location.lat;
this.locationRepository.save(weatherLocation);
}
public mapWeatherEntity(currentWeather) {
const weather = new Weather();
weather.temp = currentWeather.main.temp
weather.tempMin = currentWeather.main.temp_min
weather.tempMax = currentWeather.main.temp_max
weather.humidity = currentWeather.main.humidity;
weather.icon = currentWeather.weather[0].icon;
weather.description = currentWeather.weather[0].description;
weather.longitude = currentWeather.coord.lon;
weather.latitude = currentWeather.coord.lat;
weather.locationName = currentWeather.name;
return weather;
}
}
将条形标签设置为tag_facet
,可以通过在调用element_blank
之后调用theme
来解决。
tag_facet
由reprex package(v0.2.1)于2019-05-09创建