r - 需要帮助改进此 GIF 外观,而它在闪亮的内部运行
问题描述
我正在尝试在我闪亮的应用程序中运行 GIF。从技术上讲,我使用此代码生成 GIF 并从我的电脑在 APP 内手动上传。我的部分数据集如下所示,我正在尝试运行一个 GIF,显示 1960 年至 2015 年的前 10 部电影。
这是我的 GIF 的链接。 https://media.giphy.com/media/U4eSLRaDbGpjqU0V42/giphy.gif
这是我的应用程序图片 - ( https://imgur.com/3Ddao5S ) 我需要解决 4 个问题。
1 - 首先,当我在我的 Shiny 选项卡上运行它时,其中一些电影名称被剪掉了,你不能完全减少它们。如何固定它的方向?
2 - 我希望最后的收入显示为 1M 或 3M 或 300M 而不是 2554489 绝对不是 1e+ 554 等 有没有办法像这样设置 X 轴?
3 - 网格线很难看 AF 如何删除它们?
4 - 慢下来,怎么样?
是的,我知道我的应用程序没有直接渲染 GIF,我花了数周时间无法做到,在里面创建的 GIF 无法正常工作。
ui <- fluidPage(theme = shinytheme("cerulean"),
titlePanel(p(strong("Movie browser, 1960 - 2014", windowTitle = "Movies"))),
# Sidebar layout with a input and output definitions
sidebarLayout(
mainPanel(
tabsetPanel(
tabPanel(h4(p(strong("GIF"))), tags$img(src = "outfile.GIF"))
)
)
)
structure(list(id = c(135397L, 135397L, 76341L, 76341L, 262500L,
140607L, 140607L, 140607L, 168259L, 168259L), budget = c(150000000L,
150000000L, 150000000L, 150000000L, 110000000L, 200000000L, 200000000L,
200000000L, 190000000L, 190000000L), revenue = c(1513528810,
1513528810, 378436354, 378436354, 295238201, 2068178225, 2068178225,
2068178225, 1506249360, 1506249360), title = structure(c(3L,
3L, 4L, 4L, 2L, 5L, 5L, 5L, 1L, 1L), .Label = c("Furious 7",
"Insurgent", "Jurassic World", "Mad Max: Fury Road", "Star Wars: The Force Awakens"
), class = "factor"), homepage = structure(c(2L, 2L, 3L, 3L,
5L, 4L, 4L, 4L, 1L, 1L), .Label = c("http://www.furious7.com/",
"http://www.jurassicworld.com/", "http://www.madmaxmovie.com/",
"http://www.starwars.com/films/star-wars-episode-vii", "http://www.thedivergentseries.movie/#insurgent"
), class = "factor"), director = structure(c(1L, 1L, 2L, 2L,
5L, 3L, 3L, 3L, 4L, 4L), .Label = c("Colin Trevorrow", "George Miller",
"J.J. Abrams", "James Wan", "Robert Schwentke"), class = "factor"),
runtime = c(124L, 124L, 120L, 120L, 119L, 136L, 136L, 136L,
137L, 137L), vote_average = c(6.5, 6.5, 7.1, 7.1, 6.3, 7.5,
7.5, 7.5, 7.3, 7.3), release_year = c(2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L), genre = structure(c(1L,
2L, 1L, 2L, 2L, 1L, 2L, 4L, 1L, 3L), .Label = c("Action",
"Adventure", "Crime", "Fantasy"), class = "factor"), breakeven = c(1363528810,
1363528810, 228436354, 228436354, 185238201, 1868178225,
1868178225, 1868178225, 1316249360, 1316249360), AerageVotesCat = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Excellent",
"Good"), class = "factor")), row.names = c(NA, 10L), class = "data.frame")
UI:
# library(gifski)
library(ggplot2)
library(gganimate)
library(dplyr)
library(scales)
library(shiny)
output$plot2 <- renderImage({
df2 <- df1 %>%
group_by(release_year) %>%
# The * 1 makes it possible to have non-integer ranks while sliding
mutate(rank = rank(-revenue),
Value_rel = revenue/revenue[rank==1],
Value_lbl = paste0(" ",round(revenue))) %>%
group_by(title) %>%
filter(rank <=10) %>%
ungroup()
staticplot = ggplot(df2, aes(rank, group = title,
fill = as.factor(title), color = as.factor(title))) +
geom_tile(aes(y = revenue/2,
height = revenue,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(title, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(y=revenue,label = Value_lbl, hjust=0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x = element_line( size=.1, color="grey" ),
panel.grid.minor.x = element_line( size=.1, color="grey" ),
plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
plot.caption =element_text(size=8, hjust=0.5, face="italic", color="grey"),
plot.background=element_blank(),
plot.margin = margin(2,2, 2, 4, "cm"))
anim = staticplot + transition_states(release_year, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'Revenue ($) per Year for the top 10 movies : {closest_state}',
subtitle = "Top 10 Movies",
caption = "Revenue in USD | Data Source: Kaggle")
anim_save("outfile.gif",animate(anim, 200, fps = 4, width = 800, height = 900))
})
}
)
}
# Create the Shiny app object
shinyApp(ui = ui, server = server)
解决方案
我不知道您所有问题的答案,但请查看下面的自我可持续代码:
library(ggplot2)
library(reshape2)
library(scales)
example_df <- data.frame(movie=c("A", "B", "C", "D"),
revenue=c(3354823, 1284690, 8513581, 4699102))
x_cols <- scales::hue_pal()(length(levels(example_df$movie)))
names(x_cols) <- levels(example_df$movie)
ggplot(data=example_df) + geom_bar(aes(x=movie, y=revenue, fill=movie), stat="identity") +
scale_y_continuous(labels = dollar_format(scale=10^-6, suffix="M")) +
theme_bw() +
theme(axis.text.x = element_text(angle=45,
colour=x_cols[substr(levels(example_df$movie),1,1)]),
panel.grid = element_blank())
我将首先声明您可能会使未使用的事情过于复杂化geom_bar
。它旨在处理您想要显示的数据类型。因此,坚持下去将是有利可图的。如果您想用与条形本身相同的颜色为条形图的标签着色,您可以遵循此处显示的技巧:按组着色轴标签,我无耻地将其应用于我的示例。
广告。1.这是最难重现的部分,原因很多。一种避免这种情况的可靠方法是使用angle
函数的参数稍微旋转名称element_text
。只要您回到条形图,这将起作用。
广告。2.使用伟大的scales
图书馆。dollar_format
是您要查找的函数(尽管名称很混乱)。
广告。3.
panel.grid
在theme
功能上。
推荐阅读
- javascript - Fullcalendar 没有加载我的 json 来显示事件
- javascript - 如何将两个数组组合成一个二维数组?
- encryption - 加密具有 2 个或少数值的多个字符串
- javascript - PHP 和 Javascript 问题 - SyntaxError:不能两次声明 const 变量:'map'
- javascript - 不带引号的 JSON.stringify 键
- c# - 实现 C# 硬件内在函数包装问题
- machine-learning - (如何)我可以对已经看到的数据使用强化学习吗?
- tekton - Tekton Dashboard 是否应该部署在根路径上?
- python - Blender Python 将文本写入平面
- javascript - Javascript算法 - 网格中线条笔划最少的路径