我希望将Shiny仪表板中的ggplot与电视的背景图像对齐,以使其看起来好像该图是在电视屏幕上生成的。到目前为止,我已经能够将背景图像添加到绘图中,但是我不知道如何对齐两者。
这是我到目前为止所得到的:
相同的代码如下
m <- ggplot(data_table,aes(x = centre_x,y=centre_y))+geom_point()
+labs(y = " Screen Height",title = "TV Screen ", x="Screen Width")
m+theme_void()+ scale_fill_gradientn(colors=c('#030C54',"#7c7DBCDE"))
+ theme(axis.text=element_blank(), axis.title=element_blank(),
axis.ticks=element_blank(),
plot.title = element_text(hjust = 0.5),
panel.grid=element_blank())
+guides(fill = F)
+stat_density_2d(geom = "raster", aes(fill = ..density..), contour = F)}
+geom_rect(aes(xmin=video_data()$frame_cent_x-video_data()$framelen/4, xmax=video_data()$frame_cent_x+video_data()$framelen/4, ymin=video_data()$frame_cent_y-video_data()$frameht/4, ymax=video_data()$frame_cent_y+video_data()$frameht/4),
fill='NA',color='red')
+background_image(img)
1。如何拉伸图以与电视图像宽度对齐
如何减小绘图的高度或移动位置使其出现在电视面板中
如果对图进行修补不可行,如何调整背景图像以适合图。即如何获取绘图尺寸以修改图像尺寸
更新1 在此处添加ui.r和server.r代码
ui.r
添加代码的相关部分
tb_dashboard<-fluidPage(
fluidRow(
fluidRow(column(4,
wellPanel(
fluidRow(valueBoxOutput("TotalValue_adj",width=12))
,fluidRow(valueBoxOutput("MedianValue_100",width=12))
,fluidRow(valueBoxOutput("Total_seconds", width = 12))
,fluidRow(valueBoxOutput("Total_score", width = 12))
)
,fluidRow(sliderInput("sld_rate", "Avg. Rate per sec:",
min = 100000, max = 3000000, value = 1000000,
width='100%'
))
),
THE PLOT==>column(4,wellPanel( fluidRow(withSpinner(plotOutput('screen_exposure_locations')))
)
),
column(4,wellPanel(withSpinner(flexdashboard::gaugeOutput('percentage_gauge'))
, withSpinner(flexdashboard::gaugeOutput('score_gauge'))
, withSpinner(flexdashboard::gaugeOutput('quality_gauge'))
)
)
)
)
)
ui <- dashboardPage(
title = ('Data Analysis'),
dashboardHeader(title = img(
src = "logo.png",
height = 50,
align = "right"
)),
dashboardSidebar( shinythemes::themeSelector(),sidebar),
dashboardBody(
shinyDashboardThemes(
theme = "blue_gradient"),
tabItems(
# First tab content
tabItem(tabName = "dashboard",choiceRow,hr(),tb_dashboard),
tabItem(tabName = "details",tb_details),
tabItem(tabName = "comparison",choiceRow1,hr(),tb_comparison)
)
)
)
server.R
我已经使用ggpubr库将背景图像添加到绘图中
img <- readPNG(img.file)
output$screen_exposure_locations<-renderPlot({
m <- ggplot(data_table,aes(x = centre_x,y=centre_y))+geom_point()
+labs(y = " Screen Height",title = "TV Screen ", x="Screen Width")
m+theme_void()+ scale_fill_gradientn(colors=c('#030C54',"#7c7DBCDE"))
+ theme(axis.text=element_blank(), axis.title=element_blank(),
axis.ticks=element_blank(),
plot.title = element_text(hjust = 0.5),
panel.grid=element_blank())
+guides(fill = F)
+stat_density_2d(geom = "raster", aes(fill = ..density..), contour = F)}
+geom_rect(aes(xmin=video_data()$frame_cent_x-video_data()$framelen/4, xmax=video_data()$frame_cent_x+video_data()$framelen/4, ymin=video_data()$frame_cent_y-video_data()$frameht/4, ymax=video_data()$frame_cent_y+video_data()$frameht/4),
fill='NA',color='red')
+background_image(img)
)
谢谢